数据库连接、操作(封装使用)

来源:互联网 发布:ardupilot3.3源码下载 编辑:程序博客网 时间:2024/05/20 16:43
private static String url = "jdbc:mysql://localhost:3306/jinxiaocun";
private static String user = "root";
private static String password = "root";
//数据库连接操作

private JdbcTool() {
}


static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static Connection getCon() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

//释放资源
public static void free(ResultSet re, Statement stat, Connection con) {
try {
if (re != null)
re.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (stat != null)
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (con != null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 0
原创粉丝点击