JDBC 及数据库操作

 

import  java.sql.DriverManager;
import  java.sql.Connection;
import  java.sql.SQLException;
import  javax.sql.DataSource;
import  java.util.Properties;

public   class  DBAccess
{
 
private static DataSource dataSources = null;
 
private Properties properts = new Properties();
 
 
private String dbDriver = "oracle.jdbc.dirver.OracleDriver";
 
private String dbURL = "jdbc:oracle:thin:@130.51.56.16:1521:sid";
 
private String username = "test";
 
private String password = "test";
 
 
public  DBAccess(){
 
 }

 
 
public static Connection getConnection()throws SQLException,Exception{
  
try{
   
if(dataSources != null)
    
return dataSources.getConnection();
   
   properts.setProperties(
"user",username);
   properts.setProperties(
"password",password);
   
   Class.forName(dbDriver).newInstance();

            
return DriverManager.getConnection(dbURL,properts);  
  }
catch(SQLException e){
   System.out.println(e.getMessage());
   
throw e;
  }
catch(Exception e){
   System.out.println(e.getMessage());
   
throw e;
  }

 }

 
 
public static Connection getConnection(int i)throws SQLException,Exception{
  
try{
   
if(dataSources != null)
    
return dataSources.getConnection();
    
   Class.forName(dbDriver).newInstance();
   
   
return DriverManager.getConnection(dbURL,username,password);
  }
catch(SQLException e){
   System.out.println(e.getMessage());
   
throw e;
  }
catch(Exception e){
   System.out.println(e.getMessage());
   
throw e;
  }
 
 }

 
 
//查询
 public static void query(Connection conn,String sql)throws SQLException{
  PreparedStatement pstm 
= null;
  ResultSet rs 
= null;
  
try{
   pstm 
= conn.prepareStatement(sql);
   
//pstm.setString(1,"");
   
//pstm.setInt(2,""); 
   rs = pstm.executeQuery();
   
while(rs.next()){
    System.out.println(rs.getString(
1));
   }

  }
catch(SQLException e){
   e.printStackTrace();
   
throw e;
  }
finally{
   close(rs,pstm);
  }

 }

 
 
//更新
 public static void update(Connection conn,String sql)throws SQLException{
  PreparedStatement pstm 
= null;
  
try{
   pstm 
= conn.preparedStatement(sql);
   
//pstm.setString(1,"");
   
//pstm.setInt(2,""); 
   pstm.executeUpdate();
  }
catch(SQLException e){
   e.printStackTrace();
   
throw e;
  }
finally{
   close(pstm);
  }

 }

 
 
//批量更新
 public static void update(Connection conn,String sql,ArrayList ar1,ArrayList ar2)
 
throws SQLException{
  PreparedStatement pstm 
= null;
  
try{
   pstm 
= conn.preparedStatement(sql);
   
for (int i = 0; i < rowSet.size(); i++{
       pstm.setString(
1,(String)ar1.get(i));
    pstm.setInt(
2,Integer.parseInt((String)ar2.get(i)));
    pstm.addBatch();
   }

   pstm.executeBatch();
  }
catch(SQLException e){
   e.printStackTrace();
   
throw e;
  }
finally{
   close(pstm);
  }

 }

 
 
//调用存储过程
 public static Properties callProc(Connection conn,String procName)
 
throws SQLException{
  CallableStatement cstmt 
= null;
  Properties properties 
= new Properties();
  
try{
   cstmt 
= conn.prepareCall(procName);
   
//cstmt.setString(1,"");
   
//cstmt.setInt(2,"");
   cstmt.registerOutParameter(3,Types.NUMERIC);
   cstmt.registerOutParameter(
4,Types.VARCHAR);
   cstmt.execute();
   cstmt.commit();
   
   
//取得输出值
   String on_flag = String.values(cstmt.getInt(5)); 
         String os_msg 
= (String)cstmt.getString(6);  
         properties.setProperty(
"on_flag",on_flag);
         
if(os_msg==null)
             os_msg 
= "";
         properties.setProperty(
"os_msg",os_msg);

  }
catch(SQLException e){
   e.printStackTrace();
   
throw e;
  }
finally{
   close(cstmt);
  }

  
return Properties;
 }
 
 
 
//关闭
 public close(ResultSet rs,PreparedStatement pstm,CallableStatement cstmt,Connection conn)
 
throws SQLException{
  
if(rs != null)
   rs.close(); 
  
if(cstmt != null)
   cstmt.close();
  
if(pstm != null)
   pstm.close();
  
if(conn != null)
   conn.close();
 }

 
//关闭连接 
 public void close(Connection conn)throws SQLException{
  close(
null,null,null,conn);
 }

 
//关闭pstm
 public void close(PreparedStatement pstm)throws SQLException{
  close(
null,pstm,null,null);
 }

 
//关闭cstmt
 public void close(CallableStatement cstmt)throws SQLException{
  close(
null,null,cstmt,null);
 }

 
//关闭rs,pstm
 public void close(ResultSet rs,PreparedStatement pstm)throws SQLException{
  close(rs,pstm,
null,null);
 }

 
 
/**
 * test
 
*/

 
public static void main(String args[])
 
{
  String procName 
= "{call smp_usm_pickup_bss_p(?,?,?,?)}";
  String querySql 
= "select f_name from intf_class_t where f_id = ?,f_age = ?";
  String updateSql 
= "update intf_class_t set f_name = ? where f_id = ?,f_age = ?"
  java.sql.Connection conn 
= null;
  
try{
   conn 
= DBAccess.getConnection(0);
   
//query
   DBAccess.query(conn,querySql);
   
//update
   DBAccess.update(conn,updateSql);
   
//call proc
   DBAccess.callProc(conn,procName);
  }
catch(SQLException e){
   e.printStackTrace();
  }

 }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值