爱问知识人 爱问教育 医院库

jsp连接数据库sql server2005

首页

jsp连接数据库sql server2005

我想要用jsp来连接sql server2005,希望用的是jdbc驱动,因为我已经用那个jdbc-odbc连接成功,但觉得有点麻烦,所以希望哪位大虾能给出一个完整的jdbc连接jsp的详细代码。谢谢

提交回答
好评回答
  • 2011-07-26 10:23:31
      给你一个我很久以前写的,现在都已经不在使用这低级的用法了。建议你学学spring和hibernate,这个用起来更方便,实用。
    /*
     * DataBaseConn。java
     *
     * Created on 2006年1月26日, 上午10:19
     * 连接管理
     */
    package ccb。
      pub; import java。sql。*; import javax。sql。*; import ming。*; /** * * @author */ public final class DataBaseConn { private static int iUsed = 0; /** Creates a new instance of DataBaseConn */ private DataBaseConn() { } /* * 向数据库连接池申请一个数据连接 */ public static Connection getConn(){ Context ctx = null; DataSource ds = null; Connection cnn=null; String envString = tLinkPool(); try { ctx=new InitialContext(); if(ctx==null) throw new Exception("没有匹配的环境"); ds=(DataSource)ctx。
      lookup(envString); if(ds==null) throw new Exception("没有匹配数据库"); cnn= tConnection(); //设置连接的事务提交方式均为自动提交 tAutoCommit(true); Debug。
      logWithDetailByLevel("建立连接成功", 0, 2); iUsed++; StringBuffer msg = new StringBuffer(""); msg。
      append("连接占用:"); msg。append(iUsed); Debug。logWithDetailByLevel( String(),0,2); }catch(Exception e) { Debug。
      log(e); } return cnn; } /* * 将已经使用完毕的数据库连接返还给连接池 * 参数:conn 要返还的数据库连接 */ public static void closeConn(Connection conn){ if (conn != null){ try { ose(); Debug。
      logWithDetailByLevel("断开连接成功", 0, 2); iUsed--; StringBuffer msg = new StringBuffer(""); msg。
      append("连接占用:"); msg。append(iUsed); Debug。logWithDetailByLevel( String(),0,2); }catch(SQLException sqle){ Debug。
      log(sqle); }finally{ conn = null; } } } } /* * DataBase。
      java * * Created on 2005年12月13日, 下午6:04 * * 该类所在包为:ccb。pub。database,是一个公共类, * 为系统提供对SQL数据库连接功能。 * 使用时只需要构建这个类的实例对象就可。
       */ package ccb。pub; import java。sql。*; import ccb。pub。Consts; import ccb。pub。Debug; import ccb。pub。DataBaseConn; /* * @author */ public class DataBase { Connection conn = null; Statement stmt = null; ResultSet rs = null; public DataBase() { } /* * 执行sql插入语句 * 参数:sql:sql语句代码 * 返回:成功返回 Consts。
      SUCC,否则为Consts。ERR_DB_OP */ public static int executeInsert(String sql) { Connection conntemp = null; Statement stmttemp = null; ResultSet rstemp = null; int ret_code = Consts。
      SUCC; try { conntemp = tConn(); stmttemp = eateStatement(); stmttemp。executeUpdate(sql); ose(); stmttemp = null; Debug。
      logWithDetailByLevel(sql, 1, 2); } catch (SQLException ex) { Debug。log(ex); ret_code = Consts。
      ERR_DB_OP; }finally{ oseConn(conntemp); } return ret_code; } /* * 执行sql查询语句 * 参数:sql:sql语句代码 * 返回:成功返回 数据集,否则为 null * 注意:使用此函数将得到数据集,同时占用conn、stmt、rs等资源 * 使用此方法必须建立此类的实例 * 要求:使用完数据集后一定要调用closeConn方法释放占用资源 */ public ResultSet executeQuery(String sql) { int ret_code = Consts。
      SUCC; try { if ((conn == null) ||( Closed())){ conn = tConn(); Debug。
      logWithDetailByLevel("建立连接成功", 1, 2); } stmt = eateStatement( ResultSet。
      TYPE_SCROLL_SENSITIVE, ResultSet。CONCUR_READ_ONLY); rs = null; rs = stmt。executeQuery(sql); Debug。
      logWithDetailByLevel(sql, 1, 2); } catch (SQLException ex) { Debug。log(ex); oseConn(conn); } return rs; } /* * 执行sql更新语句 * 参数:sql:sql语句代码 * 返回:成功返回 Consts。
      SUCC,否则为Consts。ERR_DB_OP */ public static int executeUpdate(String sql) { Connection conntemp = null; Statement stmttemp = null; ResultSet rstemp = null; int ret_code = Consts。
      SUCC; try { conntemp = tConn(); stmttemp = eateStatement(); stmttemp。executeUpdate(sql); ose(); stmttemp = null; Debug。
      logWithDetailByLevel(sql, 1, 2); } catch (SQLException ex) { Debug。log(ex); ret_code = Consts。
      ERR_DB_OP; }finally{ oseConn(conntemp); } return ret_code; } /* * 执行sql删除语句 * 参数:sql:sql语句代码 * 返回:成功返回 Consts。
      SUCC,否则为Consts。ERR_DB_OP */ public static int executeDelete(String sql) { Connection conntemp = null; Statement stmttemp = null; ResultSet rstemp = null; int ret_code = Consts。
      SUCC; try { conntemp = tConn(); stmttemp = eateStatement(); stmttemp。executeUpdate(sql); ose(); stmttemp = null; Debug。
      logWithDetailByLevel(sql, 1, 2); } catch (SQLException ex) { Debug。log(ex); ret_code = Consts。
      ERR_DB_OP; }finally{ oseConn(conntemp); } return ret_code; } /* * 释放由executeQuery方法建立的stmt资源 */ public void closeStmt(){ try{ if (stmt != null) ose(); }catch (SQLException sqle){ Debug。
      log(sqle); }finally { stmt = null; } } /* * 释放由executeQuery方法建立的rs资源 */ public void closeResultSet(){ if (rs != null) { try{ ose(); }catch(SQLException sqle){ Debug。
      log(sqle); } finally{ rs = null; } } } /* * 释放由executeQuery方法建立的conn资源 */ public void closeConn(){ if (conn != null){ try{ if (rs != null) ose(); if (stmt != null) ose(); oseConn(conn); Debug。
      logWithDetailByLevel("断开连接成功", 1, 2); }catch(SQLException sqlw){ Debug。log(sqlw); }finally{ rs = null; stmt = null; conn = null; } } } } public class Configure { static private int debug = 2; /** debug级别:1 只写异常 *      2 写数据库异常和文件操作异常 *      9 写全部信息 */ static private String logPath = "C:\\"; // 日志路径,缺省时为c:\log static private String logFileName = "System_Log。
      Log"; // 日至文件名称 static private String upLoadPath = "upload"; //数据库库连接池名称 static private String linkpool = ""; //图片文件保存路径 static private String picpath = ""; /** Creates a new instance of Configure */ public Configure() { } /** * Getter for property debug。
       * @return Value of property debug。 */ public static int getDebug() { return debug; } /** * Setter for property debug。
       * @param debug New value of property debug。 */ public static void setDebug(int level) { if (level > 2){ debug = 2; }else{ debug = level; } } /** * Getter for property logPath。
       * @return Value of property logPath。 */ public static String getLogPath() { return logPath; } /** * Setter for property logPath。
       * @param logPath New value of property logPath。 */ public static void setLogPath(String LogPath) { if ((LogPath == null)||(LogPath。
      equals(""))){ LogPath = "C:\\"; } if(!LogPath。endsWith("\\")){ StringBuffer lp = new StringBuffer(LogPath); lp。
      append("\\"); LogPath = String(); } //测试路径的正确性 File dir = new File(LogPath); //如果路径不存在,在建立此路经 if((!dir。
      exists())||(! Directory())){ if(! dirs()){ //建立目录失败,就在C:\目录下建立日志文件 LogPath = "C:\\"; intln("建立日志路径失败,请联系系统管理员。
      "); } } logPath = LogPath; } /** * Getter for property logFileName。 * @return Value of property logFileName。
       */ public static String getLogFileName() { return logFileName; } /** * Setter for property logFileName。
       * @param logFileName New value of property logFileName。 */ public static void setLogFileName(String LogFileName) { if((LogFileName == null)||(LogFileName。
      equals(""))){ LogFileName = "System_Log。Log"; } //判断扩展名正确性 LogFileName = UpperCase(); if(!LogFileName。
      endsWith("。LOG")){ StringBuffer lf = new StringBuffer(LogFileName); lf。append("。LOG"); LogFileName = String(); } logFileName = LogFileName; } /* * 从xml文件中读取相关配置信息 * 参数:xmlfilename 配置文件的文件名称, 文件为xml格式 */ public static void loadConfigureFromXML(String xmlfilename){ //建立xml解析类实例 DocumentBuilderFactory dbf = wInstance(); DocumentBuilder db = null; try { db = wDocumentBuilder(); } catch (ParserConfigurationException pce) { intln(pce); //出异常时输出异常信息,不修改任何环境信息 } //建立文档读写类实例 //如果发生任何异常,将不修改系统信息,使用原有设置建立日志文件 Document doc = null; try { doc = rse(xmlfilename); }catch(SAXException saxe){ intln( tMessage()); return; } catch (DOMException dom) { intln( tMessage()); return ; } catch (IOException ioe) { intln( tMessage()); return; } //获取xml文件的根信息 Element root = tDocumentElement(); //获取日志路径 NodeList configs = tElementsByTagName("logpath"); Element config = (Element) em(0); Text t = (Text) tFirstChild(); String logpath = tNodeValue(); //获取日至文件名称 configs = tElementsByTagName("logfilename"); config = (Element) em(0); t = (Text) tFirstChild(); String logfilename = tNodeValue(); //获取debug级别信息 configs = tElementsByTagName("debug"); config = (Element) em(0); t = (Text) tFirstChild(); String debuglevelstr = tNodeValue(); int debuglevel = rseInt(debuglevelstr); //设置系统参数 setDebug(debuglevel); setLogPath(logpath); setLogFileName(logfilename); } /* * 通过debug、logpath、logfilename参数建立配置类实例 * */ public Configure(int debuglevel,String logpath,String logfilename){ setDebug(debuglevel); setLogPath(logpath); setLogFileName(logfilename); } /* * 设置上传文件目录 * 参数:上传文件目录 */ public static void setUpLoadPath(String uploadpath){ if ((uploadpath == null)||(uploadpath。
      equals(""))){ uploadpath = "C:\\"; } if(!uploadpath。endsWith("\\")){ StringBuffer lp = new StringBuffer(uploadpath); lp。
      append("\\"); uploadpath = String(); } //测试路径的正确性 File dir = new File(uploadpath); //如果路径不存在,在建立此路经 if((!dir。
      exists())||(! Directory())){ if(! dirs()){ //建立目录失败,就在C:\目录下建立日志文件 uploadpath = "C:\\"; intln("建立上传文件路径失败,请联系系统管理员。
      "); } } upLoadPath = uploadpath; } /* * 获取上传文件目录 * */ public static String getUpLoadPath(){ return upLoadPath; } /**功能:设置数据库连接池的名称 * 参数:数据库连接名称 * 返回:无 */ public static void setLinkPool(String strLinkPool){ linkpool = strLinkPool; } /**功能:获取数据库连接池名称 * 参数:无 * 返回:数据库连接赤名称 */ public static String getLinkPool(){ return linkpool; } /**功能:设置图片文件保存目录 *参数:picpath *返回:无 */ public static void setPicPath(String PicPath){ picpath = PicPath; } /**功能:获取图片文件保存目录 *参数: *返回:文件路径 */ public static String setPicPath(){ return picpath ; } 。
      

    好***

    2011-07-26 10:23:31

类似问题

换一换
  • 数据库 相关知识

  • 电脑网络技术
  • 电脑网络

相关推荐

正在加载...

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 170-189
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):