任何人都可以帮助我,为什么我无法访问ms-access数据库。 请在我的代码中查找错误。
protected String processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
int e_no=Integer.parseInt(request.getParameter("enrollment_no"));
String branch=request.getParameter("branch");
String sem=request.getParameter("semester");
String f_name=request.getParameter("first_name");
String l_name=request.getParameter("last_name");
String email=request.getParameter("email");
String pass=request.getParameter("password");
Connection con;
Statement stm;
try {
String driver="sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String db="jdbc:odbc:practicedb";
con=DriverManager.getConnection(db);
stm=con.createStatement();
String query="insert into studenttbl values='"+e_no+"','"+branch+"','"+sem+"','"+f_name+"','"+l_name+"','"+email+"''"+pass+"'";
stm.executeUpdate(query);
out.println("Database Successfuly added");
}
catch(ClassNotFoundException | SQLException e) {
}
}
return null;
}
答案 0 :(得分:0)
修改强>
String database = "FILE PATH ON YOUR LAPTOP";
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";DriverID=22;READONLY=true";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url, "", "");
java.sql.Statement stm = con.createStatement();
另外,在返回null之前添加catch statement
。
正如@jpw指出的那样,您可能需要仔细检查插入查询。它应该是这样的:
String query="insert into studenttbl values('"+e_no+"','"+branch+"','"+sem+"','"+f_name+"','"+l_name+"','"+email+"''"+pass+"')";