import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUtils {
// 构造方法私有化
private JdbcUtils()
{
}
// 注册驱动
static
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e)
{
throw new ExceptionInInitializerError();
}
}
// 创建连接
public static Connection getConnection() throws SQLException
{
return DriverManager.getConnection(url,user,password);
}
// 释放资源
public static void release(ResultSet rs ,Statement st,Connection conn)
{
try{
if (rs!=null)
rs.close();
}catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
if (st!=null)
st.close();
}catch (SQLException e)
{
e.printStackTrace();
}finally
{
try{
if(conn!=null)
conn.close();
}catch(SQLException e)
{
e.printStackTrace();
}
}
}
}
private static String url ="jdbc:mysql://localhost:3306/library";
private static String user ="root";
private static String password ="shuangyuzuo";
}
————————————————
版权声明:本文为CSDN博主「明明如月学长」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w605283073/article/details/46572485