java类——操作SqlServer数据库

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

 package tt;

import java.sql.*;

public class DB_sqlserver {
 
 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;
 
 private Statement stmt = null;
 
 public DB_sqlserver(String host, String database, String user, String password) {
 
  this.host = host;
  
  this.database = database;
  
  this.user = user;
  
  this.password = password;
  
  //显示中文
  
  this.url = "jdbc:jtds:sqlserver://" + this.host + ":1433/" + this.database ;
  
  try {
  
   Class.forName("net.sourceforge.jtds.jdbc.Driver");
  
  }
  
  catch (ClassNotFoundException e) {
   
   System.err.println("class not found:" + e.getMessage());
  
  }
  
  try {
  
   con = DriverManager.getConnection(this.url, this.user, this.password);
   stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
     ResultSet.CONCUR_READ_ONLY);
  
  //连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
  
  }
  
  catch (SQLException a) {
  
   System.err.println("sql exception:" + a.getMessage());
  
  }
 
 }

 public DB_sqlserver(String database) {//重载,直接连接默认数据库系统,只选择数据库名称
  
  this.host = "192.168.102.114";
  
  this.database = database;
  
  this.user = "sa";
  
  this.password = "richer";
  
  //显示中文
  
  this.url = "jdbc:jtds:sqlserver://" + host + ":1433/" + database;
  
  try {
  
   Class.forName("net.sourceforge.jtds.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
  
  }
  
  catch (SQLException a) {
  
   System.err.println("sql exception:" + a.getMessage());
  
  }
 
 }

/**

* 返回取得的连接

*/

 public Connection getCon() {
 
  return con;
 
 }
 
 /**

 * 创建新的状态集
 */ 
 public Statement creatStmt(){
  Statement newStmt = null;
  try {
   newStmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
     ResultSet.CONCUR_READ_ONLY);
  } catch (SQLException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }
  return newStmt;
 }
 

/**

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

* 返回取得的结果集

*/

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

/**

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

* 执行成功则返回true

*/

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

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

* 执行成功则返回true

*/

 public boolean exeInsert(String sql) {

  boolean v = false;
  //Statement stmt = creatStmt();
  try {

   v = stmt.execute(sql) ;
   v = true;
  }

  catch (SQLException e) {

   e.printStackTrace();

  }
  Func.cout(sql+"插入成功!");
  return v;

 }

 /**

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

  * 执行成功则返回true

  */

 public boolean close() {

  boolean v = false;

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

  return v;

 } 

}