我java直接连接数据库,不用tomcat加载驱动器,而是直接用java连接驱动器。

解决方案 »

  1.   

    thin驱动连接字符串:"jdbc:oracle:thin:用户名/密码@localhost:1521:实例名"
    驱动类:"oracle.jdbc.driver.OracleDriver"
      

  2.   

    public static Connection getConnection() {
    Connection connection = null; try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e1) {
    System.out.println("数据库驱动类没找到");
    }
    try {
    // 建立连接
    String serverName = "xxx.xxx.xxx.xxx"; // or 本机 "localhost"
    String portNumber = "1521";
    String sid = "database";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "name";
    String password = "password";
    connection = DriverManager.getConnection(url, username, password);
    System.out.println("数据库连接成功!");
    } catch (SQLException e) {
    System.out.println("执行SQL语句异常!");
    e.printStackTrace();
    }
    return connection;
    }
      

  3.   

    这些我都试了,怎么都不行,这是我测试的代码
    import java.sql.*;
    public class jdbc{
    //定义驱动程序的名称
    public static final String drivername="oracle.jdbc.driver.OracleDriver";
    public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
    public static final String user="scott";
    public static final String password="aaa";
    public static void main(String args[]){
    String query="select ename,sal from scott.emp where deptno=20";
    Connection conn=null;
    Statement st=null;
    ResultSet rs=null;
    try{
    //加载驱动器
      Class.forName(drivername);
      //建立连接
      conn=DriverManager.getConnection(URL,user,password);
      //创建statement对象
      st=conn.createStatement();
      //
      rs=st.executeQuery(query);
      while(rs.next())
      {
       System.out.println(rs.getString("ename")+"as"+rs.getString("sal"));
       }
       rs.close();
       st.close();
       conn.close();
    }
    catch(Exception e){
    System.out.println("this is error");
    }
    }

    }
      

  4.   


    除了"this is error" 还能报什么错误,这样的调试神仙也没有办法.
      

  5.   

    catch(Exception e){
         System.out.println("this is error");
    } 这个不好,数据库连接可能出现异常的点非常多,但就简单的输出"this is error",怎么确定问题?问题都不知道,怎么解决?
         System.out.println(e);
       或者 e.printStackTrace(System.out);
      

  6.   

    是的,按十楼的说的办,这样让系统自动选择报错,找到根结所在。你看一下你的jar包class12.jar(9i)是否导入了,另外确认下你的数据库名是否正确,从服务里面可以找到