jdbc 工具类 JdbcUtils  简化数据…

来源:互联网 发布:淘宝网首页的分类 编辑:程序博客网 时间:2024/05/16 00:41

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 (ClassNotFoundExceptione)
   {
    thrownew ExceptionInInitializerError();
        
 }
// 创建连接
 publicstatic Connection getConnection() throws SQLException
 {
  returnDriverManager.getConnection(url,user,password);
 }
// 释放资源
 publicstatic void release(ResultSet rs ,Statement st,Connectionconn)
 {
  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";

}

0 0