Java连接MYSQL数据库类

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();
  }
 }
}

你可能感兴趣的:(java,mysql,数据库,职场,休闲)