首页 > 编程 > JSP > 正文

JSP实战型程序连载:通用数据库连接JavaBean

2024-09-05 00:19:29
字体:
来源:转载
供稿:网友

package online;

 

import java.sql.*;

 

public class dbconn {

  private static string rootpath = "web发布路径";

  private string sample = "sample";

  private connection con = null;

  private statement stmt = null;

  resultset rs = null;

  /***************************************************************/

  private static final string drive = "sun.jdbc.odbc.jdbcodbcdriver";

  //暂时使用jdbc-odbc连接//"com.microsoft.jdbc.sqlserver.sqlserverdriver";

  private static final string username = "sa";

  private static final string password = "123aaa";

  private static final string host = "http:127.0.0.1:8080/renshi";

  /*************************************************************/

  //暂时使用jdbc-odbc数据源

  private static final string connection_string = "jdbc:odbc:renshi";

  //"jdbc:microsoft:sqlserver://localhost;1433;";

  public static string getrootpath() {

    return rootpath;

  }

 

  public dbconn() { //加载驱动

    try {

      class.forname(drive);

    }

    catch (classnotfoundexception e) {

      system.err.println("dbconn():" + e.tostring());

    }

    catch (exception e) {

      system.err.println("dbconn():" + e.tostring());

    }

  }

 

  public connection getconnection() { //得到连接

    try {

      string strurl = connection_string;

      /***********周五晚改动****************************************/

      //+ "datebasename=renshi," + username +"," + password;

      con = drivermanager.getconnection(strurl, this.username, this.password);

    }

    catch (exception e) {

      con = null;

    }

    return con;

  }

 

  public void dropconnection() { //关闭连接

    try {

      closestmt();

      con.close();

    }

    catch (exception ignored) {

    }

    finally {

      con = null;

    }

  }

 

  public resultset executequery(string sql) { //执行sql查询

    resultset rs = null;

    try {

      con = getconnection();

      stmt = con.createstatement(resultset.type_scroll_sensitive,

                                 resultset.concur_read_only);

      rs = stmt.executequery(sql);

    }

    catch (sqlexception ex) {

      system.err.println("dbconn.executequery():" + ex.getmessage());

    }

    return rs;

  }

 

  public int executeupdate(string sql) { //执行sql更新语句

    int i=0;

    stmt = null;

    rs = null;

    try {

      con = getconnection();

      stmt = con.createstatement();

     i= stmt.executeupdate(sql);

      stmt.close();

      con.close();

    }

    catch (sqlexception ex) {

      system.err.println("dbconn:executeupdate(0:" + ex.getmessage());

    }

    return i;

  }

 

  public void execute(string sql) { //执行sql语句

    stmt = null;

    rs = null;

    try {

      con = getconnection();

      stmt = con.createstatement();

      stmt.execute(sql);

      stmt.close();

      con.close();

    }

    catch (sqlexception ex) {

      system.err.println("dbconn:excute():" + ex.getmessage());

    }

  }

 

  public void closeconn() { //关闭sql连接

    try {

      stmt.close();

    }

    catch (sqlexception e) {

      e.printstacktrace();

    }

  }

 

  public void closestmt() { //关闭sql连接

    try {

      con.close();

    }

    catch (sqlexception e) {

      e.printstacktrace();

    }

 

  }

 

  /**

   * main

   */

  public static void main(string[] args) throws sqlexception {

    dbconn one=new dbconn();

    resultset rs=one.executequery("select * from ps_info");

    while(rs.next()){

      system.out.println(rs.getstring(2));

    }

  }

 

}

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表