数据库连接方法总结

自我总结的数据库连接方法

(以查询全部为例)

一、传统连接

1.类DBConnection ,用于得到Connection 对象

public class DBConnection {
 private final String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
 private final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JSP" ;
 private final String DBUSER = "sa" ;
 private final String DBPASSWORD = "123456" ;
 private Connection conn = null ;
 
 public DBConnection()
 {
  try
  {
   Class.forName(DBDRIVER) ;
   this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ; 
  }
  catch (Exception e)
  {
  }
 }

 // 取得数据库连接
 public Connection getConnection()
 {
  return this.conn ;
 }

 // 关闭数据库连接
 public void close()
 {
  try
  {
   this.conn.close() ;
  }
  catch (Exception e)
  {
  }  
 }

}
2.查询全部的代码

public List queryAll() throws Exception {
  // TODO Auto-generated method stub
  List all = new ArrayList() ;
  String sql = "SELECT id,title,author,content FROM note" ;
  PreparedStatement pstmt = null ;
  DBConnection dbc = null ;
  dbc = new DBConnection() ;
  try
  {
   pstmt = dbc.getConnection().prepareStatement(sql) ;
   ResultSet rs = pstmt.executeQuery() ;
   while(rs.next())
   {
    Note note = new Note() ;
    note.setId(rs.getInt(1)) ;
    note.setTitle(rs.getString(2)) ;
    note.setAuthor(rs.getString(3)) ;
    note.setContent(rs.getString(4)) ;
    all.add(note) ;
   }
   rs.close() ;
   pstmt.close() ;
  }
  catch (Exception e)
  {
   System.out.println(e) ;
   throw new Exception("操作中出现错误!!!") ;
  }
  finally
  {
   dbc.close() ;
  }
  return all ;

 }

 

 

二、采用数据源连接

1.类DBConnection ,用于得到Connection 对象

public class DBConnection {
 
 private Connection conn = null ;
 
 public DBConnection()
 {
  try
  {

      Context initContext=new InitialContext();

      Context context=(Context)initContext.lookup("java:/comp/env");

      DataSource ds=(Context)context.lookup("jdbc/sqlds");
      this.conn = ds.getConnection(); 
  }
  catch (Exception e)
  {
  }
 }

 // 取得数据库连接
 public Connection getConnection()
 {
  return this.conn ;
 }

 // 关闭数据库连接
 public void close()
 {
  try
  {
   this.conn.close() ;
  }
  catch (Exception e)
  {
  }  
 }

}
2.查询全部的代码(同“一”中)

 

三、采用Commons-DbUtils组件(不应用数据源)

 

public class DBConnection {
 private final String DBDRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver" ;
 private final String DBURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=JSP" ;
 private final String DBUSER = "sa" ;
 private final String DBPASSWORD = "123456" ;
 private Connection conn = null ;
 
 public DBConnection()
 {
  try
  {
    
DbUtils.loadDriver(DBDRIVER ) ;

     this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ; 
  }
  catch (Exception e)
  {
  }
 }

 // 取得数据库连接
 public Connection getConnection()
 {
  return this.conn ;
 }

  }

}

 

 

 

 

2.查询全部的代码

 

public List queryAll() throws Exception {
  // TODO Auto-generated method stub
  List all = new ArrayList() ;
  String sql = "SELECT id,title,author,content FROM note" ;

  DBConnection dbc = new DBConnection() ;

  Connection conn=dbc.getConnection();

  QueryRunner qr=new QueryRunner();

  all=(List)qr.query(conn,sql,new BeanListHandler(Note.class));

  return all ;

 }

 

 

 

 

四、采用Commons-DbUtils组件(应用数据源)

1.类DBDataSource ,用于得到DataSource 对象

public class DBDataSource {

 private DataSource ds = null ;
 
 public DBDataSource()
 {
 

      Context initContext=new InitialContext();

      Context context=(Context)initContext.lookup("java:/comp/env");

 

      ds=(Context)context.lookup("jdbc/sqlds");
  
 }

 

 // 取得数据源

 public DataSource getDataSource()
 {
  return this.ds ;
 }

 

}

2.查询全部的代码

public List queryAll() throws Exception {
  // TODO Auto-generated method stub
  List all = new ArrayList() ;
  String sql = "SELECT id,title,author,content FROM note" ;

  DBDataSource dbc = new DBDataSource() ;

  DataSource ds=dbc.getDataSource();

  QueryRunner qr=new QueryRunner(ds);

  all=(List)qr.query(sql,new BeanListHandler(Note.class));

  return all ;

 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; public class DBConnection { /** * 获得与数据库的连接 * * @param path * @return Connection */ public static Connection getConn(String classDriver, String url, String user, String pwd) { try { Class.forName(classDriver); return DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(String jndiName) { try { Context ctx; ctx = new InitialContext(); DataSource dataSource = (DataSource) ctx.lookup("java:comp/env/" + jndiName); return dataSource.getConnection(); } catch (NamingException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } public static Connection getConn(Properties properties) { try { String driver = properties.getProperty("jdbc.driverClassName"); String url = properties.getProperty("jdbc.url"); String user = properties.getProperty("jdbc.username"); String password = properties.getProperty("jdbc.password"); Class.forName(driver); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } return null; } /** * oracle连接 * * @param path * @return Connection */ public static Connection getOracleConn(String
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值