1.导入jar包
2.数据库连接字符串
public static final String URL="jdbc:mysql://localhost:3306/xuanke?&useSSL=false&serverTimezone=UTC";
3.加载驱动:Class.forName(驱动路径)
Class.forName("com.mysql.cj.jdbc.Driver");
4.创建数据库的连接
try {
conn=DriverManager.getConnection(URL,NAME,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
5.执行sql语句
public static void main(String[] args) {
System.out.println(DBHelper.getconn());
}
}
6…关闭连接
public void realease(Connection coon) {
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}```