我测试了一些解析并得到了字符串。现在我想把它写到本地数据库,看看能否在更大的规模上这样做。我应该选择什么数据库,这是容易处理的?第一步是什么?
/编辑。我和eclipse一起工作。我对编程还是个新手。安装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来处理你的数据库