zoukankan      html  css  js  c++  java
  • java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 与 java.sql.SQLException: Listener refused the connection with the following error: 及”java连接oracle数据库“

    第一个java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver是没有导入ojdbc[x].ja文件。

    第二个java.sql.SQLException: Listener refused the connection with the following error:  是你数据源中url配置有问题。如下:

        请打开F:appAdmin_yfdsouproduct11.2.0dbhome_1NETWORKADMIN 

     java连接oracle示例代码:

    package org.lanqiao.test;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class ConnTest {
        public Connection conn=null;  //数据库连接类
        public Statement stmt=null;  //执行sql
        public ResultSet rs=null;   //保存查询结果
    
    
        //oracle
        private static String dbDriver="oracle.jdbc.driver.OracleDriver";  
        private static String dbUrl="jdbc:oracle:thin:@127.0.0.1:1521:orcl";    
        //mysql5.7以下    
    //    private static String dbDriver="com.mysql.jdbc.Driver";  
    //    private static String dbUrl="jdbc:mysql://127.0.0.1:3306/mybatis";
        private static String dbUser="scott";
        private static String dbPwd="3333";
        
        //连接数据库
        public static Connection getConnection(){
            Connection conn=null;
            try{
                Class.forName(dbDriver); //加载驱动
                conn=DriverManager.getConnection(dbUrl, dbUser, dbPwd);    
            }catch(Exception e){
                e.printStackTrace();
            }
            if(conn!=null){
                System.out.println("数据库连接success");
            }else {
                System.err.println("警告:数据库连接失败");
            }
            return conn;
        }
        
        //执行查询select
        public ResultSet doQuery(String sql){
            try{
                conn=ConnTest.getConnection();
                stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
                rs=stmt.executeQuery(sql);  //执行select
            }catch(Exception e){
                e.printStackTrace();
            }
            return rs;
        }
        
        //执行新增、修改、删除insert  update delete
        public int doUpdate(String sql){
            int result=0;
            try{
                conn=ConnTest.getConnection();
                stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
                result=stmt.executeUpdate(sql);
            }catch(Exception e){
                e.printStackTrace();
            }
            return result;
        }
        
        //关闭数据库连接
        public void closeConnection(){
            //先关闭结果集rs
            try{
                if(rs!=null){
                    rs.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            //再关闭Statement
            try{
                if(stmt!=null){
                    stmt.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            //最后关闭connection
            try{
                if(conn!=null){
                    conn.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args){
            ConnTest connDB=new ConnTest();
            connDB.getConnection();
            String sql="Insert into scott.LANQIAO_INFOS values(10,'sz',1,1,1,1)";
            int result=connDB.doUpdate(sql);
            if(result>0) {
                System.out.println("insert ok");
            }
            
        }
        
        
    }
  • 相关阅读:
    ActiveMQ JBDC巨坑记录:java.sql.SQLException: Cannot create PoolableConnectionFactory (Communications link failure,Cannot create PoolableConnectionFactory (Communications link failure
    请求https接口时的SSLHandshakeException
    MySQL授权命令grant注意事项
    一次docker服务启动失败的总结
    zabbix-agent和zabbix-agent2的区别,zabbix-agent的主动和被动模式
    MySQL alter修改语句
    zabbix监控tcp的11种状态
    MySQL创建数据库并且插入数据
    MySQL的基础语法
    设置MySQL密码
  • 原文地址:https://www.cnblogs.com/zjazn/p/14199740.html
Copyright © 2011-2022 走看看