【发布时间】:2013-10-25 01:58:16
【问题描述】:
我在 Netbeans 7.2 上遇到此错误,它表示 ClassNotFoundexception 和 InstantationException。我真的被这件事困住了。请帮助我。
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String driver = "com.mysql.jdbc.Driver";
con = null;
String username = "";
String password = "";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbName", "root", "password");
Statement st = con.createStatement();
ResultSet mar = st.executeQuery("SELECT * FROM table");
Gson gson = new GsonBuilder().create();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
} catch (SQLException e) {
String message = e.getMessage();
}
【问题讨论】:
-
首先,您不需要
.newInstance()电话。其次,您是否将mysql-connector.jar添加到您的WEB-INF/lib以便将其添加到类路径中? -
为什么 .newInstance() ????
-
第三,你也不需要 .forName() 调用。您的问题所剩无几。