通过这种方式,您可以连接到数据库:
public Connection getConnection() throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);
if (this.dbms.equals("mysql")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + "://" +
this.serverName +
":" + this.portNumber + "/",
connectionProps);
} else if (this.dbms.equals("derby")) {
conn = DriverManager.getConnection(
"jdbc:" + this.dbms + ":" +
this.dbName +
";create=true",
connectionProps);
}
System.out.println("Connected to database");
return conn;
}
更多在这里http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html,您也可以在 youtube 上搜索一些教程,只需输入:java datebase connection。Hibernate 框架可以帮助您。您还应该知道 JPA/JDBC。