package jsu.code.smile.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DButil {
private static String dbDriver="com.mysql.jdbc.Driver"; //定义dbDriver——数据库驱动
//static PreparedStatement pst=null;
//private static ResultSet rs=null;
private static String url="jdbc:mysql://localhost:3306/computer"; //定义数据库的url 、username 、password
private static String username="root";
private static String password="123456";
public static Connection getConnection(){ //静态方法 实现连接
Connection conn=null;
try{
Class.forName(dbDriver);
conn=(Connection) DriverManager.getConnection(url,username,password);
}catch(Exception e){
e.printStackTrace();
}
System.out.println("已获得数据库连接");
return conn;
}
public static void main(String args[]){
Connection conn=DButil.getConnection();//静态方法
System.out.println(conn);
}
注意:还要导入外部包 mysql-connector-java-5.1.7-bin