一直用的oracle的数据库,今天做测试的时候想用mysql,写了个DBConnect
代码如下:
1 public class DBConnect {
2 public static Connection getConnection() {
3 Connection conn = null;
4 String urlstr ="jdbc:mysql://127.0.0.1:3306/database";
5 try{
6 Class.forName("com.mysql.jdbc.Driver");
7 conn = DriverManager.getConnection(urlstr,"username","password");
8 } catch(ClassNotFoundException ex) {
9 ex.printStackTrace();
10 return null;
11 }
12 return conn;
13 }