Java操作oracle数据库

2 篇文章 0 订阅
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6.   
  7. import javax.naming.Context;  
  8. import javax.naming.InitialContext;  
  9. import javax.naming.NamingException;  
  10. import javax.sql.DataSource;  
  11.   
  12. public class DBCon {  
  13.     //数据库驱动对象  
  14.     public static final String DRIVER="oracle.jdbc.driver.OracleDriver";  
  15.     //数据库连接地址(数据库名)  
  16.     public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";  
  17.     //登陆名  
  18.     public static final String USER="FM";  
  19.     //登陆密码  
  20.     public static final String PWD="FM";  
  21.     //创建数据库连接对象  
  22.     private Connection con=null;  
  23.     //创建数据库预编译对象  
  24.     private PreparedStatement ps=null;  
  25.     //创建结果集  
  26.     private ResultSet rs=null;  
  27.     //创建数据源对象  
  28.     public static DataSource source=null;  
  29.   
  30. //  //静态代码块  
  31. //  static{  
  32. //  
  33. //      //初始化配置文件context  
  34. //      try {  
  35. //          Context context=new InitialContext();  
  36. //          source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage");  
  37. //      } catch (Exception e) {  
  38. //          // TODO Auto-generated catch block  
  39. //          e.printStackTrace();  
  40. //      }  
  41. //  
  42. //  
  43. //  }  
  44.   
  45.         /**  
  46.          * 获取数据库连接  
  47.          */  
  48.         public Connection getCon(){  
  49.             try {  
  50.                 Class.forName(DRIVER);  
  51.             } catch (ClassNotFoundException e) {  
  52.                 // TODO Auto-generated catch block  
  53.                 e.printStackTrace();  
  54.             }  
  55.             try {  
  56.                 con=DriverManager.getConnection(URL,USER,PWD);  
  57.             } catch (SQLException e) {  
  58.                 // TODO Auto-generated catch block  
  59.                 e.printStackTrace();  
  60.             }  
  61.               
  62.             return con;  
  63.         }  
  64. //  /**  
  65. //   * 获取数据库连接  
  66. //   */  
  67. //  public Connection getCon(){  
  68. //  
  69. //      try {  
  70. //          con=source.getConnection();  
  71. //      } catch (SQLException e) {  
  72. //          // TODO Auto-generated catch block  
  73. //          e.printStackTrace();  
  74. //      }  
  75. //  
  76. //      return con;  
  77. //  }  
  78.   
  79.   
  80.     /**  
  81.      * 关闭所有资源  
  82.      */  
  83.     public void closeAll(){  
  84.         if(rs!=null)  
  85.             try {  
  86.                 rs.close();  
  87.             } catch (SQLException e) {  
  88.                 // TODO Auto-generated catch block  
  89.                 e.printStackTrace();  
  90.             }  
  91.             if(ps!=null)  
  92.                 try {  
  93.                     ps.close();  
  94.                 } catch (SQLException e) {  
  95.                     // TODO Auto-generated catch block  
  96.                     e.printStackTrace();  
  97.                 }  
  98.                 if(con!=null)  
  99.                     try {  
  100.                         con.close();  
  101.                     } catch (SQLException e) {  
  102.                         // TODO Auto-generated catch block  
  103.                         e.printStackTrace();  
  104.                     }  
  105.   
  106.   
  107.     }  
  108.     /**  
  109.      * @param sql数据库更新(增、删、改) 语句      
  110.      * @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)  
  111.      * @return 返回受影响都行数  
  112.      */  
  113.     public int update(String sql,String... pras){  
  114.         int resu=0;  
  115.         con=getCon();  
  116.         try {  
  117.             ps=con.prepareStatement(sql);  
  118.             for(int i=0;i<pras.length;i++){  
  119.                 ps.setString(i+1,pras[i]);  
  120.             }  
  121.             resu=ps.executeUpdate();  
  122.         } catch (SQLException e) {  
  123.             // TODO Auto-generated catch block  
  124.             e.printStackTrace();  
  125.         }  
  126.         finally{  
  127.             closeAll();  
  128.         }  
  129.         return resu;  
  130.     }  
  131.   
  132.     /**  
  133.      * @param sql数据库查询语句  
  134.      * @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)  
  135.      * @return 返回结果集  
  136.      */  
  137.     public ResultSet query(String sql,String... pras){  
  138.         con=getCon();  
  139.         try {  
  140.             ps=con.prepareStatement(sql);  
  141.   
  142.             if(pras!=null)  
  143.                 for(int i=0;i<pras.length;i++){  
  144.                     ps.setString(i+1, pras[i]);  
  145.                 }  
  146.             rs=ps.executeQuery();  
  147.         } catch (SQLException e) {  
  148.             // TODO Auto-generated catch block  
  149.             e.printStackTrace();  
  150.         }  
  151.         return rs;  
  152.     }  
  153.   
  154.   
  155. }  

 

转载自:http://blog.csdn.net/zhuangjixiang/article/details/8998469

orcle一条sq插入多条数据:insert into table(column...)  select value1,...valuen from dual union select .........from dual

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值