且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java连接MYSQL数据库类

更新时间:2022-09-22 09:53:15

package Ge.ConnectionDB;
import java.sql.*;
public class Condb {
 String driver="com.mysql.jdbc.Driver";
 String url="jdbc:mysql:3306//172.32.34.253/gecris";
 String user="root";
 String passwd="root";
 Connection conn=null;
 ResultSet rs=null;
 Statement st=null;
 public Condb(){
  try {
   Class.forName(driver);
  }catch(ClassNotFoundException e){
   e.printStackTrace();
  }
 }
 //in order to update and insert operation
 public int update(String sql){
  int result=0;
  try{
   conn=DriverManager.getConnection(url,user,passwd);
   st=conn.createStatement();
   result=st.executeUpdate(sql);
  }catch (SQLException e){
   e.printStackTrace();
  }
  return result;
 }
 //in order to select operation
 public ResultSet query(String sql){
  try{
   conn=DriverManager.getConnection(url,user,passwd);
   st=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
   rs=st.executeQuery(sql);
  }catch (SQLException e){
   e.printStackTrace();
  }
  return rs;
 }
 //in order to delete operation
 public int del(String sql){
  int DelResult=0;
  try{
   conn=DriverManager.getConnection(url,user,passwd);
   st=conn.createStatement();
   DelResult=st.executeUpdate(sql);
  }catch (SQLException e){
   e.printStackTrace();
  }
  return DelResult;
 }
 //in order to close with database
 public void close(){
  try{
   if (rs!=null)
    rs.close();
  }catch(Exception e){
   e.printStackTrace();
  }
  try{
   if (conn!=null)
    conn.close();
  }catch(Exception e){
   e.printStackTrace();
  }
 }

}

本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/291545如需转载请自行联系原作者


world77