国产探花免费观看_亚洲丰满少妇自慰呻吟_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));

    }

  }

 

}

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延寿县| 叶城县| 绥化市| 柘城县| 泽库县| 来宾市| 南开区| 通城县| 廊坊市| 定日县| 白玉县| 盘锦市| 玉山县| 涪陵区| 烟台市| 沙湾县| 余庆县| 铜鼓县| 平顶山市| 东阿县| 胶州市| 十堰市| 施秉县| 正蓝旗| 恩施市| 南澳县| 宁强县| 凯里市| 博罗县| 项城市| 万安县| 新乡县| 府谷县| 六枝特区| 互助| 南丹县| 隆回县| 剑阁县| 房产| 陈巴尔虎旗| 邮箱|