原创  JDBC连接MySQL公共类 收藏

 package com.gaulhor.jdbc;

import java.sql.*;

public final class mysqlConnectionBase
{
 private final static String url="jdbc:mysql://localhost:3306/iseeuon";
 private final static String user="root";
 private final static String password="";
 
 private mysqlConnectionBase(){}
 
 static
 {
  try
  {
   //注册驱动
   Class.forName("com.mysql.jdbc.Driver");
  }
  catch(ClassNotFoundException e)
  {
   throw new ExceptionInInitializerError(e);
  }
 }
 
 public static Connection getConnection() throws SQLException
 {
  //建立连接
  return DriverManager.getConnection(url,user,password);
 }
 
 public  static void free(ResultSet rs,Statement st,Connection conn)
 {
  //6.释放资源
   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();
     }
    }
    
   }
 }
}

发表于 @ 2009年03月03日 11:04:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:Sql分页查询 | 新一篇:MD5加密

  • 发表评论
  • 评论内容:
  •  
Copyright © gaulhor
Powered by CSDN Blog