国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > 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));

    }

  }

 

}

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 磐石市| 泸定县| 南漳县| 利川市| 德化县| 醴陵市| 黎城县| 贺州市| 突泉县| 淅川县| 江口县| 青田县| 成都市| 定结县| 新民市| 林州市| 上思县| 武冈市| 库尔勒市| 沅江市| 锦州市| 唐海县| 玛沁县| 临城县| 泗洪县| 南溪县| 富裕县| 泾阳县| 景宁| 封丘县| 南雄市| 且末县| 黑山县| 巴青县| 花莲市| 文山县| 东兰县| 灵石县| 黑水县| 连山| 阜康市|