Java连接oracle数据库

在连接oracle数据库是我们可以这样的做,但需要加载oracle数据库的驱动

 

classes12.jar (1,600,090 bytes) - for use with JDK 1.2 and JDK 1.3
在低级JDK版本1.21.3中使用的驱动,虽然实际上在1.41.5中使用大部分情况也是OK

连接数据库的那个jar,  比如ojbc14.jar,或者class12.zip那样的东西,没有加入到jar的配置文件里面去。可以仿照那个第三方jar放置的路径,在自己打的那个jar包里面那个配置文件,就是那指定main class的那个文件里面,把数据库这个jar给加上去

 

 

packagecom.c.tools;

 

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.PreparedStatement;

importjava.sql.ResultSet;

importjava.sql.SQLException;

 

/*

 * 访问oracle的通用类

 */

public class OracleDBHelper {

         private String driveClassName="oracle.jdbc.driver.OracleDriver";

         private String url="jdbc:oracle:thin:@192.168.131.49:1521:Test2749";

         //private String url="jdbc:oracle:thin:@localhost:1521:XE";

         private String userid="scott";

         private String userpassword="tiger";

         private Connection conn=null;

         privatePreparedStatementpsmt=null;

         privateResultSetrs=null;

         booleanisAutoCommit=true;

        

         public        OracleDBHelper(){

                   //加载驱动

                   try {

                            Class.forName(driveClassName);

                   } catch (ClassNotFoundException e) {

                            System.out.println("加载驱动失败!");

                            e.printStackTrace();

                   }

         }

        

         /*

          * 创建连接

          */

         public Connection getConnection(){

                   try {

                            conn=DriverManager.getConnection(url, userid, userpassword);

                   } catch (SQLException e) {        

                            System.out.println("建立到目标的连接失败!");

                            e.printStackTrace();

                   }

                  

                   return conn;

         }

        

         /*

          * 执行insert,update,delete操作

          */

         publicintexecuteUpdate(String sql,Object[] params){

                   inti=0;

                   if(conn==null){

                            getConnection();

                   }

                  

                   try {

                            //创建PreparedStatement

                            psmt=conn.prepareStatement(sql);

                            //传递参数

                            if(params!=null){

                                     for(intidx=0;idx<params.length;idx++){

                                               //这里可以做防注

                                               psmt.setObject(idx+1, params[idx]);

                                     }

                            }

                            //执行

                            i=psmt.executeUpdate();

                           

                   } catch (SQLException e) {                          

                            e.printStackTrace();

                   }finally{

                            closeStatement();

                            closeConnection();

                   }

                  

                  

                   returni;

        

         }

        

         /*

          * 带有事务的操作

          */

         publicintexecuteUpdateByTransaction(String sql,Object[] params) throws SQLException{

                   inti=0;      

                  

                   try {

                            //创建PreparedStatement

                            psmt=conn.prepareStatement(sql);

                            //传递参数

                            if(params!=null){

                                     for(intidx=0;idx<params.length;idx++){

                                               //这里可以做防注

                                               psmt.setObject(idx+1, params[idx]);

                                     }

                            }

                            //执行

                            i=psmt.executeUpdate();

                           

                   } catch (SQLException e) {                          

                            throw e;

                   }                

                  

                   returni;

        

         }

        

         /*

          * 执行select操作

          */

         publicResultSetexecuteQuery(String sql,Object[] params){                     

                   if(conn==null){

                            getConnection();

                   }

                                    

                   try {                   

                            //创建PreparedStatement

                            psmt=conn.prepareStatement(sql);

                            //传递参数

                            if(params!=null){

                                     for(intidx=0;idx<params.length;idx++){

                                               //这里可以做防注

                                               psmt.setObject(idx+1, params[idx]);

                                     }

                            }

                            //执行

                            rs=psmt.executeQuery();

                           

                   } catch (SQLException e) {                          

                            e.printStackTrace();

                   }                

                  

                   returnrs;

        

         }

        

         /*

          * 关闭连接

          */

         public void closeConnection(){                  

                   try {

                            if(conn!=null){

                                     conn.close();

                                     conn=null;

                            }

                   } catch (SQLException e) {        

                            System.out.println("关闭Connection失败!");

                            e.printStackTrace();

                   }

         }

        

         /*

          * 关闭Statement

          */

         public void closeStatement(){          

                   try {

                            if(psmt!=null){

                                     psmt.close();

                                     psmt=null;

                            }

                   } catch (SQLException e) {        

                            System.out.println("关闭Statement失败!");

                            e.printStackTrace();

                   }

         }

        

         /*

          * 关闭ResultSet

          */

         public void closeResultSet(){            

                   try {

                            if(rs!=null){

                                     rs.close();

                                     rs=null;

                            }

                   } catch (SQLException e) {        

                            System.out.println("关闭ResultSet失败!");

                            e.printStackTrace();

                   }

         }

        

         /*

          * 是否开启事务

          */

         public void openTransaction(booleanisOpen){

                   isAutoCommit=!isOpen;

                  // 是否开启事务

                   try {

                            //创建连接

                            if(conn==null){

                                     conn=getConnection();

                            }

                            conn.setAutoCommit(isAutoCommit);

                   } catch (SQLException e) {                          

                            e.printStackTrace();

                   }

         }

        

         /*

          * 提交事务

          */

         public void commitTransaction(){

                   if(!isAutoCommit){

                            try {

                                     conn.commit();

                            } catch (SQLException e) {                                    

                                     e.printStackTrace();

                            }                          

                   }

         }

        

         //回滚事务

         public void rollbackTransaction(){

                   if(!isAutoCommit){

                            try {

                                     conn.rollback();

                            } catch (SQLException e) {                                    

                                     e.printStackTrace();

                            }

                   }

         }

        

        

}

你可能感兴趣的:(java,oracle,sql,数据库,String,null)