Java连接MySQL数据库,我写了个直接返回连接对象的方法:
- static Connection getMySqlConnection()
- {
-
- try
- {
-
- String strConnString = "jdbc:mysql://localhost:3306/newsdb2?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8";
- Class.forName ("com.mysql.jdbc.Driver");
- return DriverManager.getConnection (strConnString);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }