栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我使用哪种简单的数据库解决方案将解析的变量从eclipse提取到数据库

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我使用哪种简单的数据库解决方案将解析的变量从eclipse提取到数据库

您可以使用MySQL数据库。

1)下载mysql连接器,然后将jar添加到您的项目中。

2)在db中创建一个表,并使用该表在其中插入值。

数据库的Java代码

public class Database {    private Connection connection = null;    private Statement statement = null;    private String serverName = null;    private String dbName = null;    private String  dbtablename = null;    private String username = null;    private String password = null;   public Database() {        serverName = //your value;        dbName = your value        dbtablename = //your value        username = //your value        password = //your value    }   public Connection OpenConnectionDB() {        try { Class.forName("com.mysql.jdbc.Driver").newInstance(); this.connection = DriverManager.getConnection("jdbc:mysql://" + serverName + "/" + dbName + "?useUnipre=yes&characterEncoding=UTF-8&" + "user=" + username + "&password=" + password); System.out.println("conection: " + connection); // this.connection = DriverManager.getConnection("jdbc:mysql://" + serverName + "/" + dbName  +"?user=" + username + "&password=" + password ); //System.out.println("debug: Connect to dbServer");        } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        } catch (InstantiationException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        } catch (IllegalAccessException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        } catch (ClassNotFoundException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        }        return connection;    }    public void closeConnectionDB() {        if (this.connection != null) { try {     connection.close();     connection = null;     //System.out.println("debug: Close dbServer"); } catch (SQLException ex) {     System.out.println("debug: Closing Database... FAILED (NOT RUNNING)");     Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); }        }    }    public void insert(){         String query = //your insert query         OpenConnectionDB();         try { this.statement = this.connection.createStatement();     this.statement.execute(query);     statement.close();        } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        }        closeConnectionDB();    }   public String getResults(){        String query = //select query        OpenConnectionDB();        try { this.statement = this.connection.createStatement(); ResultSet  rs = this.statement.executeQuery(query); if(rs!=null){     while(rs.next()){         return rs.getString("Your_field_name_in_db"); //get your      }  }  statement.close();        } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);        }        closeConnectionDB();        return null;    }}

编辑 您也可以使用MySQL
WorkBench
处理您的数据库。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/414336.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号