标签:
package com.jdbc.dao; import java.sql.*; public class BaseDAO { //打开数据库链接 public Connection getConn() { Connection conn = null; try { //加载驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //打开链接 conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = epetDB","sa","sa"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //(重写)关闭链接 public void Close(Connection conn,PreparedStatement pstmt,ResultSet rs) { try { //关闭结果集 if (rs != null) { rs.close(); } //关闭PerparedStatement对象 if (pstmt != null) { pstmt.close(); } //关闭链接 if (conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception } } //(重写)关闭链接 public void Close(Connection conn,PreparedStatement pstmt) { try { //关闭PerparedStatement对象 if (pstmt != null) { pstmt.close(); } //关闭链接 if (conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception } } //增删改操作 public int Update(String sql,Object[] parm) { int iRet = 0; Connection conn = null; PreparedStatement pstmt = null; try { conn = getConn(); pstmt = conn.prepareStatement(sql); //循环赋值参数 for (int i = 0; i < parm.length; i++) { //为预编译sql设置参数 pstmt.setObject(i+1, parm); } //执行SQL语句 iRet = pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { Close(conn,pstmt); } return iRet; } }
普及:
try{ //可能出现异常的代码 }catch(Execption e){ //如果发生异常处理的代码 }finally{ //无论是否异常都会执行的代码 try catch finally java中异常处理机制
我们来分析一下写一段代码,其中Update方法是用来更新数据的,其中我们可以看到try中包含了getConn()方法用来获取Connection连接对象,到最后我们可以在finally代码块中看到Close()方法用来关闭创建的Connection对象以及PreparedStatement对象,这么消耗我们很大的内存空间。
如果用户同时点注册按钮那么服务器首先执行打开数据库连接Connection多个用户注册就会打开多个Connection那么并且同时添加到数据库,服务器就会在执行添加的时候就会发生异常。分不清楚用户注册的信息。
举个例子:
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class BaseDao { private String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private String url = "jdbc:sqlserver://localhost:1433;databasename=SQLTMP"; private String user = "sa"; private String pwd = "sa"; private static Connection conn = null; private BaseDao(){ try { Class.forName(className); conn = DriverManager.getConnection(url,user,pwd); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static Connection getConn(){ if(conn != null){ return conn; }else{ new BaseDao(); return conn; } } }
context.xml <Resource name="news" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="1000" username="sa" password="sa" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=NewsManagerSystem" /> Web.xml <resource-ref> <description>news DataSource</description> <res-ref-name>news</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> package com.news.dao; import java.sql.*; import javax.naming.*; import javax.sql.DataSource; public class BaseDao { /** * 创建连接池 * */ public Connection getConn(){ Connection conn = null; try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/news"); conn = ds.getConnection(); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } }
普及:
标签:
原文地址:http://www.cnblogs.com/ichunqiu/p/5740885.html