package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtils {
//数据库地址
private static String url="jdbc:mysql://localHost:3306/make";
//mysql的驱动
private static String driveName="com.mysql.jdbc.Driver";
//数据库用户名
private static String user="root";
//数据库密码
private static String password="root1";
/*static{
String url="jdbc:mysql://localHost:3306/make";
String driveName="com.mysql.jdbc.Driver";
String user="root";
String password="root1";
}*/
//数据库连接方法
public static Connection getConnection() throws SQLException, ClassNotFoundException{
Connection connection = null;
Statement statement =null;
//注入驱动
Class.forName(driveName);
// 建立连接 试图建立到给定数据库 URL 的连接。
return connection = DriverManager.getConnection(url, user, password);
}
//关闭连接
public static void close(PreparedStatement preparedStatement,Connection connection){
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
打开App,阅读手记