我正在尝试执行此代码&它显示成功执行但我无法获得我创建的任何值访问数据库文件。 所以,请在我的代码中找到错误,以便我可以在数据库表中获得一些值。
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename ="D:\\Database for Mini Project\\Database21(example).mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Database for Mini Project\\Database21(example).mdb";
try (Connection con = DriverManager.getConnection( database ,"","")) {
Statement s = con.createStatement();
String b=request.getParameter("uname");
String c=request.getParameter("pass");
String query="insert into A.ABC1(uname,pass) values='"+b+"','"+c+"'";
s.executeQuery(query);
s.close();
答案 0 :(得分:0)
答案 1 :(得分:0)
试试这个
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename ="D:\\Database for Mini Project\\Database21(example).mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\Database for Mini Project\\Database21(example).mdb";
String b=request.getParameter("uname");
String c=request.getParameter("pass");
try {
// connects to the database
Connection con = DriverManager.getConnection( database ,"","")) {
Statement s = con.createStatement();
String query="insert into A.ABC1(uname,pass) values(?,?);
PreparedStatement statement = conn.prepareStatement(query);
s.setString(1, b);
s.setString(2, c);
int row = s.executeUpdate();
if (row > 0) {
message = "Query executed";
}
} catch (SQLException ex) { //catch ur msaccess exception..I use mysql
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (con != null) {
// closes the database connection
try {
con.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
我使用mysql作为我的数据库..所以只要有catch(SQLException ex)将其替换为msaccess异常..