数据库连接错误JDBC

时间:2013-07-25 15:11:13

标签: java sql database jdbc

我正在使用Oracle Express Edition来处理JAVA / SQL中的项目。代码没有编译错误,但是当我尝试运行它时,我收到一条错误消息:

call.XE.SYS.DBMS_FEATURE_TEST_PROC_1

失败

这是我的代码:

 import java.sql.*;
 import javax.sql.*;

 public class StudentAverages {
 public static void main(String[] args) {

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:driver_type:[username/password]@//host_name:port_number:SID");
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("Select * from [Sheet1$]");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();


        while (rs.next()) {

            for (int i = 1; i <= numberOfColumns; i++) {
                if (i > 1)
                    System.out.print(", ");
                String columnValue = rs.getString(i);
                System.out.print(columnValue);
            }
            System.out.println("");
        }

        st.close();
        con.close();

    } catch (Exception ex) {
        System.err.print("Exception: ");
        System.err.println(ex.getMessage());
    }



}
        public void StudentMean(Connection con, Statement st, ResultSet rs) throws SQLException{

              try{
         Statement stOne, stTwo, stThree, stFour;
         String SelectAverage = "SELECT MEAN FROM STUDENTS";
          ResultSet rsOne = stOne.executeQuery(SelectAverage);

    String TotalAverage = "SELECT Avg(MEAN) AS averages FROM STUDENTS";
          ResultSet rsTwo = stTwo.executeQuery(TotalAverage);     

          String student_stan_dev = "SELECT STDEV(MEAN) AS standardDeviation FROM STUDENTS";
          ResultSet rsThree = stThree.executeQuery(student_stan_dev);

           int onesdMean = 1;


           //Loop Duration_Sec column
           while(rsOne.next()){

           //Convert values into float values
            allAvgs = rsOne.getFloat("MEAN");
            totalAvg = rsTwo.getFloat("averages"); 
            StDev = rsThree.getFloat("standardDeviation");
           }

                    //Calculate one standard deviation away from mean  
                    theSD = allAvgs - (onesdMean * StDev);


                      //Create a new column
        String flaggedStudents = "ALTER TABLE STUDENTS ADD FlaggedStudents INT";
        ResultSet rsFour = stFour.executeUpdate(flaggedStudents);

           if(allAvgs >= theSD){

                           //Flag students who meet the criteria
               String FlagHint = "INSERT INTO STUDENTS.FlaggedStudents VALUES('1')";
               st.executeUpdate(FlagHint);
           }

                    //Count the students who meet the criteria and add those students 
                    //to a new table

        String countInstances = "SELECT STUDENTS.NAME, STUDENTS.FlaggedStudents" +
                "COUNT(*)OVER(PARTITION BY STUDENTS) AS cnt FROM STUDENTS";
           st.executeQuery(countInstances);


        st.executeUpdate("CREATE TABLE IF NOT EXISTS StudentCount" +
        "(NAME INT , cnt INT)");

        String insertVals = String.format("INSERT INTO StudentCount" +
                "(NAME , cnt INT") +
                " VALUES ('%s','%s')");

        st.execute(insertVals);
}
    catch(SQLException e){
     e.printStackTrace();
    }

}

导致此错误的原因是什么,以及如何让我的项目运行?我正在使用一个瘦的驱动程序。任何帮助,将不胜感激。

enter image description here

Stack Trace:
    java.lang.ClassNotFoundException: oracle.jdbc.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 StudentAverages.main(StudentAverages.java:23)
exception :
java.sql.SQLException: No suitable driver found for jdbc:oracle:driver_type:[username/password]@//host_name:port_number:SID
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at StudentAverages.main(StudentAverages.java:31)

2 个答案:

答案 0 :(得分:1)

ALTER TABLE学生添加FlaggedStudents INT
CREATE TABLE IF NOT EXISTS StudentCount“+”(NAME INT ,cnt INT
INSERT INTO StudentCount“+”(NAME,cnt INT

以上命令不适用于在Oracle上运行。不支持INT数据类型,IF NOT EXISTS子句是MySql语法。
首先更正SQL命令并检查它们是否可以在某些oracle客户端上运行,如SQLDeveloper或Toad,然后在java代码中尝试它们。

答案 1 :(得分:0)

您应该尝试以下步骤

  1. 检查Oracle驱动程序(ojdbc6.jar或ojdbc14.jar)在项目类路径中是否可用。当您使用Oracle 10g或11g时,应使用ojdb6.jar。
  2. 更改这些行

       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    

       Class.forName("oracle.jdbc.driver.OracleDriver");
    
  3. 并检查数据库网址格式,参见参考link