2011-08-22 108 views
2

我試圖在Oracle 10g數據庫中使用預準備語句插入數據,但執行下面給出的代碼時出現「SQL異常:常規錯誤」。我認爲問題是在DATE字段或PASSWORD字段數據檢索。請幫助我解決這個問題。謝謝。使用JDBC將數據插入Oracle數據庫的問題

學生表: -

     Sid  VARCHAR2(200) PRIMARY KEY CHECK(Sid>0), 
         Pass_word VARCHAR2(10) NOT NULL, 
         S_name VARCHAR2(20) NOT NULL, 
         G_name VARCHAR2(20)   , 
         Branch VARCHAR2(10) NOT NULL, 
         D_company VARCHAR2(20)   , 
         B_Percent INT NOT NULL CHECK(B_Percent<100), 
         twelth_percent INT NOT NULL CHECK(twelth_percent<100), 
         tenth_percent INT NOT NULL CHECK(tenth_percent<100), 
         Certify VARCHAR2(30), 
         Semester INT NOT NULL CHECK(Semester<9), 
         D_Birth DATE NOT NULL, 
         Sex  VARCHAR2(6) NOT NULL 

代碼: -

int bpercent ; 
    int twelthpercent; 
    int tenthpercent; 
    int semester; 
    String studentID = null; 
    String studentpassword = null; 
    String studentname = null; 
    String Gname = null; 
    String branch = null; 
    String dcompany = null; 
    String certify = null; 
    String sex = null; 
    Date date = new Date(00-00-0000); 

    Connection connection = null; 

嘗試 {

// Load the JDBC driver 

String driverName = "sun.jdbc.odbc.JdbcOdbcDriver"; 

Class.forName(driverName); 

connection = DriverManager.getConnection("jdbc:odbc:placement","siddharth","sid"); 

studentID = StudentID.getText(); 
spassword = PasswordField.getPassword(); 
studentname = NameField.getText(); 
Gname = GuardianField.getText(); 
branch = BranchField.getText(); 
dcompany = DcompanyField.getText(); 
bpercent = Integer.parseInt(BtechField1.getText()); 
twelthpercent = Integer.parseInt(TwelthField.getText()); 
tenthpercent = Integer.parseInt(TenthField.getText()); 
semester =Integer.parseInt(SemesterField.getText()); 
certify = CertificationField.getText(); 
date = (Date) DateTextField.getValue(); 
sex = SexCombo.getActionCommand(); 



PreparedStatement state = connection.prepareStatement("insert into student " +"(sid,pass_word,s_name,g_name,branch,d_company,b_percent,twelth_percent,tenth_percent,certify,semester,d_birth,sex)"+ 
      "values(?,?,?,?,?,?,?,?,?,?,?,?,?)"); 

state.setString(1, studentID); 
state.setString(2, spassword.toString()); 
state.setString(3,studentname); 
state.setString(4,Gname); 
state.setString(5,branch); 
state.setString(6,dcompany); 
state.setInt(7,bpercent); 
state.setInt(8,twelthpercent); 
state.setInt(9,tenthpercent); 
state.setInt(10,semester); 
state.setString(11,certify); 
state.setDate(1, new java.sql.Date(date.getTime())); 
state.setString(12,sex); 

state.executeUpdate(); 
state.close(); 

JOptionPane.showMessageDialog(null,"Data Inserted","Information Messgage",JOptionPane.INFORMATION_MESSAGE); 
connection.close(); 
}           

catch (ClassNotFoundException e) { 
// Could not find the database driver 
    JOptionPane.showMessageDialog(null,e); 
} 
catch (SQLException e) { 
// Could not connect to the database 
    JOptionPane.showMessageDialog(null,e); 
} 

}

+0

發佈錯誤的堆棧跟蹤。 – Kal

+0

@kal - 我正在使用netBeans 7.0,並且在執行此操作時,netBeans中沒有出現錯誤的堆棧跟蹤。我認爲問題是DATE格式與數據庫不匹配,但不知道如何糾正它。你請檢查並告訴你是否可以。謝謝 –

回答

2

你得證明,並學期輪

在INSERT SQL字符串錯誤的方式:

insert into (... tenth_percent,certify,semester,d_birth, ...) 

state.setInt(9,tenthpercent); 
state.setInt(10,semester); 
state.setString(11,certify); 
state.setDate(12, new java.sql.Date(date.getTime())); 

所以它試圖設置學期列轉換爲字符串,這是無效的。

3

我相信你有一個錯字,你有:

state.setDate(1, new java.sql.Date(date.getTime())); 
state.setString(12,sex); 

我想你想:

state.setDate(12, new java.sql.Date(date.getTime())); 
state.setString(13,sex); 
+0

謝謝你糾正我,但我已經糾正了這個錯誤,現在它給錯誤說:「java.sql.SQLException:[Oracle] [ODBC] [Ora] ORA-01722:無效號碼」。現在我該怎麼做。請幫忙。謝謝 –

0

而不是使用行:

`String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";  
Class.forName(driverName);` 

使用行:

DriverMana ger.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver);

有時,類不能被驅動程序管理器讀取,因此您需要向drivermanager註冊驅動程序。

祝你好運!

好吧!有一個在代碼中的錯誤... 相反:

DriverManager.registerDriver(新sun.jdbc.odbc.JdbcOdbcDriver中);

用途:

DriverManager.registerDriver(新sun.jdbc.odbc.JdbcOdbcDriver中());

+0

仍然錯誤仍然存​​在。現在我能做什麼? –