我是这种 java 语言的新手,所以我仍然很难弄清楚,虽然我没有错误,但仍然没有运行。帮我弄清楚这件事。我发布了与此相同的程序,尽管问题早在我已经弄清楚了。
public Connection dbConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename =
"C:/Documents and Settings/gebs/Desktop/Database1.mdb";// fileplace
String database =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";// connection driver
database += filename.trim() + ";DriverID=22;READONLY=true}";
return DriverManager.getConnection(database, "", "");
try
{
Connection con = dbConnection();
String lname = this.jTextField1.getText();
String fname = this.jTextField2.getText();
String mname = this.jTextField3.getText();
String address = this.jTextField4.getText();
String birthday =
(String) this.jComboBox1.getSelectedItem()
+ this.jTextField5.getText() + this.jTextField6.getText();
String gender = (String) this.jComboBox2.getSelectedItem();
String age = this.jTextField7.getText();
String contactno = this.jTextField8.getText();
String contactperson = this.jTextField9.getText();
String department = this.jTextField11.getText();
String position = this.jTextField12.getText();
// to insert to database
String query =
"INSERT INTO Personal_Category "
+ "(Last_Name,First_Name,Middle_Initial,Address, "
+ "Birthday,Gender,Age,Contact_No,Contact_Person,"
+ "Department,Position) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,)";
PreparedStatement pre;
pre = con.prepareStatement(query);
pre.setString(1, lname);
pre.setString(2, fname);
pre.setString(3, mname);
pre.setString(4, address);
pre.setString(5, birthday);
pre.setString(6, gender);
pre.setString(7, age);
pre.setString(8, contactno);
pre.setString(9, contactperson);
pre.setString(10, department);
pre.setString(11, position);
pre.execute();// execute
con.close();
}
catch (Exception e)
{
System.out.println(e);
}
}