现在数据库一般交给Hibernate去管理,有时为了写个测试程序,往往要速度,而不希望大费周章地去配置hibernate。
Java原生态连接MySQL的代码,以便快速创建测试代码。
mysql-connector-java-5.1.7-bin.jar
:http://ishare.iask.sina.com.cn/f/10826345.htmlimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLConnection {
private static Connection conn = null;
public static Connection mySQLConnection() {
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/carmarket";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123456";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}