以下代码:
public void insertNewStudent(int id, String pass, String fname, String lname, String street, String city, String state, int Zip, String Email, double GPA) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:RegistrationDB", "", "");
String query = "INSERT INTO Students (ID, Password, FirstName, LastName, Street, City, State, Zip, EMail, GPA)" + "VALUES (?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, id);
ps.setString(2, pass);
ps.setString(3, fname);
ps.setString(4, lname);
ps.setString(5, street);
ps.setString(6, city);
ps.setString(7, state);
ps.setInt(8, Zip);
ps.setString(9, Email);
ps.setDouble(10, GPA);
ps.executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
引发以下异常:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
java.sql.DriverManager.getConnection(DriverManager.java:579)
java.sql.DriverManager.getConnection(DriverManager.java:221)
business.studentDB.insertNewStudent(studentDB.java:53)
controller.registercontrol.doPost(registercontrol.java:47)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
这是如何引起的?如何解决?
答案 0 :(得分:1)
添加finally
块
PreparedStatement ps = null; // declare outside the try block
try {
// ...
} catch (Exception e) {
// ...
} finally {
try {
if (ps != null) ps.close();
if (conn != null) conn.close();
} catch (SQLException e) {
// ...
}
}
让Connection
保持打开状态可能无法将更改提交到数据库。如果正在共享Connection
,则您无法明确关闭commit()
您的更改。
ps.executeUpdate();
conn.commit();
编辑:根据共享的堆栈跟踪,请确保您的Java IDE,Microsoft Access和JVM或JDK都是相同的位,即32位或64位版本。但是,是的,添加finally
块仍然是 推荐。
答案 1 :(得分:1)
因此,更改您的异常处理会发现您的真正问题,这似乎是:
The specified DSN contains an architecture mismatch between the Driver and Application
这基本上告诉您有32位驱动程序对64位访问或其他方式。您需要找出哪个并使用正确的。有关this thread上此问题的更多信息,例如。