jsp 数据库问题 希望给给指点下
<%@ page language="java" import="java.util.*, java.sql.*" pageEncoding="GB18030"%><%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
String password2 = request.getParameter("password2");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql//localhost:3306/mabin", "root", "123");
String sqlQuery = "select count(*) from user where username = ?";
PreparedStatement psQuery = conn.prepareStatement(sqlQuery);
psQuery.setString(1, username);
ResultSet rs = psQuery.executeQuery();
rs.next();
int count = rs.getInt(1);
if(count > 0) {
response.sendRedirect("registerFail.jsp");
psQuery.close();
conn.close();
return;
}
String sql = "insert into user values (null, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
ps.close();
conn.close();
response.sendRedirect("registerSuccess.jsp");
%>
总报如下错误
Servlet.service() for servlet jsp threw exception
java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mabin
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at org.apache.jsp.registerDeal_jsp._jspService(registerDeal_jsp.java:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
--------------------编程问答-------------------- java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mabin
应该是jar包的问题(是否放到项目中) ,或者是你的driver没有写对 --------------------编程问答-------------------- 驱动包放到common\lib下 --------------------编程问答-------------------- No suitable driver found for jdbc:mysql//localhost:3306/mabin
表示没有合适的驱动可以提供给要触发的数据库连接
加载jar包???有木有? --------------------编程问答-------------------- java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mabin
从这个看 应该是包导错了或者是没有导 或者是驱动名写错了
还有pageEncoding="GB18030" 这个编码是不是写错了 至少我是没看到过这样的编码方式 --------------------编程问答-------------------- 从表面上看,这是一个驱动JAR包,没放对的问题 这是书上的小例子么 --------------------编程问答-------------------- 百度有很多的这种问题的解决方式。。。 --------------------编程问答-------------------- 包貌似没导错啊 还有与编码有关系吗 --------------------编程问答-------------------- 差不多吧 老师课堂上讲的 --------------------编程问答-------------------- java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mabin
包导错,或driver写的不对 --------------------编程问答-------------------- 写错了:jdbc:mysql//localhost:3306/mabin。应该是jdbc:mysql://localhost:3306/mabin,少了一个分号 --------------------编程问答-------------------- 少了数据库的驱动包 放在webroot的web inf下的lib目录下 --------------------编程问答--------------------
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(
"jdbc:mysql://localhost/smslib", "smslib", "smslib"); } catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
另外,你的mysql-connector-java-5.1.18-bin.jar要导入到项目中YOUR_PROJECT/WEB-INF/lib/mysql-connector-java-5.1.18-bin.jar下面.
希望能帮到你. --------------------编程问答--------------------
/*
* 连接mysql数据库,要先把mysql的jdbc驱动放到工程的WEB-INF/lib包里面。
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/smslib", "smslib", "smslib");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
补充:Java , Web 开发