java类——操作MySql数据库

来源:互联网 发布:js特效网站 编辑:程序博客网 时间:2024/04/30 15:01

 package tt;

import java.sql.*;

 

public class DB_mysql {

 private String user = "";//用户名
 
 private String password = "";//密码
 
 private String host = "";//主机
 
 private String database = "";//数据库名字
 
 /*
 private String url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
 */
 
 private String url ="";
 
 private Connection con = null;
 
 Statement stmt;

 public DB_mysql(String host, String database, String user, String password) {
 
  this.host = host;
  
  this.database = database;
  
  this.user = user;
  
  this.password = password;
  
  //显示中文
  
  this.url = "jdbc:mysql://" + this.host + "/" + this.database +
  
  "?useUnicode=true&characterEncoding=GB2312";
  
  try {
  
   Class.forName("com.mysql.jdbc.Driver");
  
  }
  
  catch (ClassNotFoundException e) {
   
   System.err.println("class not found:" + e.getMessage());
  
  }
  
  try {
  
   con = DriverManager.getConnection(this.url, this.user, this.password);
  
  //连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
  
   stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  
  ResultSet.CONCUR_READ_ONLY);
  
  }
  
  catch (SQLException a) {
  
   System.err.println("sql exception:" + a.getMessage());
  
  }
 
 }

 public DB_mysql(String database) {//重载,直接连接默认数据库系统,只选择数据库名称
  
  this.host = "localhost";
  
  this.database = database;
  
  this.user = "root";
  
  this.password = "";
  
  //显示中文
  
  this.url = "jdbc:mysql://" + host + "/" + database +
  
  "?useUnicode=true&characterEncoding=GB2312";
  
  try {
  
   Class.forName("com.mysql.jdbc.Driver");
  
  }
  
  catch (ClassNotFoundException e) {
   
   System.err.println("class not found:" + e.getMessage());
  
  }
  
  try {
  
   con = DriverManager.getConnection(this.url, this.user, this.password);
  
  //连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
  
   stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  
  ResultSet.CONCUR_READ_ONLY);
  
  }
  
  catch (SQLException a) {
  
   System.err.println("sql exception:" + a.getMessage());
  
  }
 
 }

/**

* 返回取得的连接

*/

 public Connection getCon() {
 
  return con;
 
 }

/**

* 执行一条简单的查询语句

* 返回取得的结果集

*/

 public ResultSet exeQuery(String sql) {
 
  ResultSet rs = null;
  
  try {
  
   rs = stmt.executeQuery(sql);
  
  }
  
  catch (SQLException e) {
  
   e.printStackTrace();
  
  }
  
  return rs;
 
 }

/**

* 执行一条简单的更新语句

* 执行成功则返回true

*/

 public boolean exeUpdate(String sql) {
 
  boolean v = false;
  
  try {
  
   v = stmt.executeUpdate(sql) > 0 ? true : false;
  
  }
  
  catch (SQLException e) {
  
   e.printStackTrace();
  
  }
  
  return v;
 
 }
/**

* 执行一条简单的更新语句

* 执行成功则返回true

*/

 public boolean exeInsert(String sql) {

  boolean v = false;

  try {

   v = stmt.execute(sql) ;

  }

  catch (SQLException e) {

   e.printStackTrace();

  }

  return v;

 }

 /**

  * 执行一条简单的更新语句

  * 执行成功则返回true

  */

 public boolean close() {

  boolean v = false;

  try{
              
         if(stmt!=null){
          stmt.close();
          stmt=null;
         }
       
         if(con!=null){
          con.close();
          con=null;
         }
        }
        catch(Exception ee){
         System.out.println("数据库无法关闭,请检查!");
         ee.printStackTrace();
        }

  return v;

 } 

}

 

原创粉丝点击