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:\app\Admin_yfdsou\product\11.2.0\dbhome_1\NETWORK\ADMIN 

 

 

 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");
        }
        
    }
    
    
}

 

posted @ 2020-12-28 01:02  小庄的blog  阅读(1351)  评论(0编辑  收藏  举报