问题描述
2)在db中创建一个表,并使用该表在其中插入值。
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处理您的数据库。
解决方法
我测试了一些解析,并得到了字符串。现在,我想将其写入本地数据库,以查看以后是否可以在更大范围内进行操作。我选择哪种数据库容易处理?第一步是什么?
/编辑。我用日食工作。而且我对编程还很陌生。安装了MS SQL Express,但是我不知怎么处理。不知道从哪里开始…
非常感谢。