zoukankan      html  css  js  c++  java
  • 连接Oracle数据库代码

    连接Oracle数据库代码

      连接oracle数据库的代码如下:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class JdbcOracle {

     public static Connection conn=null;
     public static PreparedStatement pstamt=null;
     public static ResultSet rs=null;
     private static String driver="oracle.jdbc.driver.OracleDriver";
     private final static String url="jdbc:oralce:thin:192.168.0.109@lucs109";
     private final static String username="lucs";
     private final static String pwd="lucs";
     
     static{
      try {
       Class.forName(driver);
      } catch (ClassNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }
     
     public void getConn(){
      try {
       conn=DriverManager.getConnection(url, username, pwd);
      } catch (SQLException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       System.out.println("连接失败!");
      }
     }
     
     public void closeAll(){
      try{
      if(conn!=null){
       conn.close();
      }
      if(!rs.isClosed()||rs!=null){
       rs.close();
      }
      if(pstamt!=null){
       pstamt.close();
      }
      }catch (Exception e) {
       // TODO: handle exception
       e.printStackTrace();
      }
     }
     
     public int execAllSql(String sql,Object...trop){
      getConn();
      int count=0;
      try {
       pstamt=conn.prepareStatement(sql);
       for (int i = 0; i < trop.length; i++) {
        try {
         pstamt.setObject(i, trop[i]);
        
        } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
       }
       count=pstamt.executeUpdate();
      } catch (SQLException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      }
      return count;
     }
     public static void main(String[] args) {
      JdbcOracle j=new JdbcOracle();
      j.getConn();
     }
    }

  • 相关阅读:
    spring六种种依赖注入方式
    HibernateDaoSupport 源码
    ServiceStack.Redis常用操作
    ServiceStack.Redis 之 IRedisTypedClient
    ServiceStack.Redis之IRedisClient
    Windows下Redis的安装使用
    为什么要使用SLF4J而不是Log4J
    每天一个linux命令:mkdir
    浅谈Redis及其安装配置
    Solr4.4的安装与配置
  • 原文地址:https://www.cnblogs.com/zjiacun/p/2916920.html
Copyright © 2011-2022 走看看