初学者,仅以记录自己的日常代码,就当是笔记,方便查看。也希望对您有帮助
package com.connection;
import java.sql.Connection;import java.sql.DriverManager;
public class Conn {
Connection con=null;
public Connection getCon(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/cars?characterEncoding=UTF-8";
String username = "root";
String password = "123";
con= (Connection) DriverManager.getConnection(url,username,password);
}
catch (Exception e)
{
System.out.println("连接失败");
}
return con;
}
}
这样就创建了Connection对象,即用Conn.getCon()即可获得。