2 an mao an_mao 于 2015.06.24 23:51 提问

java连接Oracle时出错,架包什么的都导入了,哪错了啊,还得交作业呢,请大神解救

public class JavaToOracle01 {

  public static void main(String[] args){
   String strQuery="SELECT g_ID,t_ID,g_Name,g_Number FROM SCOTT.GOODS";
   Connection conn;
   Statement stat;
   ResultSet rs;
   String gid,tid,gname;
   int gnumber;
  try
   {
     //注册驱动程序
      Class.forName("oracle.jdbc.driver.OracleDriver");
    //获得和Oracle数据库的连接
    conn=DriverManager.getConnection("jdbc:odbc:orcldsn","system","Oracle123");
    stat=conn.createStatement();
   //向Oracle数据库发送SQL请求
   rs=stat.executeQuery(strQuery());
    //操作结果集对象
   while(rs.next())
    {
      gid=rs.getString(1);
      tid=rs.getString(2);
      gname=rs.getString(3);
      gnumber=rs.getInt(4);
      System.out.println(gid+","+tid+","+gname+","+gname);
     }
     //关闭相关对象
    rs.close();
    stat.close();
    conn.close();
    }
    catch(Exception err)
     {
       err.printStackTrace();
     }
}

private static String strQuery() {
    // TODO Auto-generated method stub
    return null;
}

}

报错信息:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.zpark.b4.zhouly.JavaToOracle01.main(JavaToOracle01.java:21)

2个回答

MFC123CSDN
MFC123CSDN   2015.06.25 00:50

getConnection()第一个参数URL格式写的不对url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";//127.0.0.1是本机地址(如果数据库在自己pc上,可以直接写localhost),1521为数据库端口,XE是Oracle的SID. 还有就是最好PreparedStatement。

weixin_32927447
weixin_32927447   2015.11.21 18:49

加载不到类。首先看运行时的classpath 是否没指定 没包含对应的oracle驱动jar包

Csdn user default icon
上传中...
上传图片
插入图片