[英]What simple DB-solution do I use to extract my parsed variables from eclipse to a DB
我測試了一些解析,並得到了字符串。 現在,我想將其寫入本地數據庫,以查看日后是否可以在更大范圍內進行該操作。 我選擇哪種數據庫容易處理? 第一步是什么?
/編輯。 我用日食工作。 而且我對編程還很陌生。 安裝了MS SQL Express,但我不知怎么處理。 不知道從哪里開始...
非常感謝。
您可以使用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 + "?useUnicode=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處理您的數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.