使用JDBC将数据插入Oracle数据库时出现问题

时间:2011-08-22 17:52:02

标签: java

我正在尝试使用oracle 10g数据库中的预准备语句插入数据,但在执行下面给出的代码时,我得到“SQL异常:常规错误”。我认为问题是DATE字段或PASSWORD字段数据检索。请帮我解决这个问题。感谢。

学生表: -

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

}

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

你的认证和学期是错误的方式

在插入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())); 

所以它试图将semester列设置为一个无效的字符串。

答案 2 :(得分:0)

而不是使用这些行:

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

使用以下行:

  

DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver);

有时候,驾驶员管理员无法读取该类,因此您需要使用drivermanager注册该驱动程序。

最好的运气!

嘛!代码中有错误...... 代替:

  

DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver);

使用:

  

DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());