使用JDBC将数据插入Oracle数据库的问题
我试图在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);
}
}
你得证明,并学期轮
在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()));
所以它试图设置学期列转换为字符串,这是无效的。
我相信你有一个错字,你有:
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);
谢谢你纠正我,但我已经纠正了这个错误,现在它给错误说:“java.sql.SQLException:[Oracle] [ODBC] [Ora] ORA-01722:无效号码”。现在我该怎么做。请帮忙。谢谢 –
而不是使用行:
`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中());
仍然错误仍然存在。现在我能做什么? –
发布错误的堆栈跟踪。 – Kal
@kal - 我正在使用netBeans 7.0,并且在执行此操作时,netBeans中没有出现错误的堆栈跟踪。我认为问题是DATE格式与数据库不匹配,但不知道如何纠正它。你请检查并告诉你是否可以。谢谢 –