我正在html上制作一个简单的注册页面,以存储通过JDBC输入到MySQL的数据。问题是我无法建立与数据库的连接。
这是我的jsp页面。当我打印con时,它返回null。
<%@page import="javax.servlet.jsp.tagext.TryCatchFinally"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="bean.BeanClass"%>
<%@page import="dbconnect.DatabaseConfigure"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
BeanClass a = new BeanClass();
a.setName(request.getParameter("name"));
a.setGender(request.getParameter("gender"));
a.setContact(request.getParameter("contact"));
a.setCity(request.getParameter("city"));
a.setCountry(request.getParameter("country"));
String Skillset = "";
String Skills[] = request.getParameterValues("skill");
for (String i : Skills) {
Skillset = Skillset + i + " ";
}
Connection con = DatabaseConfigure.dbconnect();
out.print(con);
PreparedStatement ps = con.prepareStatement("insert into inf values(?,?,?,?,?,?)");
ps.setString(1, a.getName());
ps.setString(2, a.getGender());
ps.setString(4, a.getCity());
ps.setString(5, Skillset);
ps.setLong(3, Long.parseLong(a.getContact()));
ps.setString(6, a.getCountry());
int nora = 0;
nora = ps.executeUpdate();
if (nora == 1) {
out.println("Account created Succesfully");
} else {
out.println("Account not created ");
}
%>
<h3>
Name:<%=a.getName()%></h3>
<h3>
Gender:<%=a.getGender()%></h3>
<h3>
Contact:<%=a.getContact()%></h3>
<h3>
City:<%=a.getCity()%></h3>
<h3>
Name:<%=Skillset%></h3>
<h3>
Name:<%=a.getCountry()%></h3>
</body>
</html>
这是我在dbconnect软件包中用于数据库连接的代码。如果我单独运行,则连接成功。
package dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConfigure {
private static Connection con;
private static final String url = "jdbc:mysql://localhost:3306/cs";
private static final String userid = "root";
private static final String password = "root";
public static Connection dbconnect() {
try {
con = DriverManager.getConnection(url, userid, password);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
System.out.println(DatabaseConfigure.dbconnect());
}
}
这是我的html页面:
<head>
<meta charset="ISO-8859-1">
<title>INFORMATION</title>
</head>
<body>
<form action="SignUp.jsp" method="post">
<input type="text" name="name" placeholder="NAME:"><br>
<br> <input type="radio" name="gender" value="M">Male<br>
<br> <input type="radio" name="gender" value="F">Female<br>
<br> <input type="number" name="contact"
placeholder="CONTACT NO:"><br>
<br> <input type="email" name="email" placeholder="E-MAIL"><br>
<br> <input type="text" name="city" placeholder="CITY"><br>
<br> <label>SKILLS:</label> <input type="checkbox" name="skill"
value="Java">Java <input type="checkbox" name="skill"
value="Python">Python <input type="checkbox" name="skill"
value="Javascript">Javascript<br>
<br> <label>COUNTRY:</label><br> <select name="country">
<option value="India">India</option>
<option value="China">China</option>
<option value="USA">USA</option>
</select> <input type="submit" name="submit">
</form>
</body>
</html>
在JSP中连接失败的原因可能是什么? 这是完整的堆栈跟踪:
SEVERE: Servlet.service() for servlet [jsp] in context with path [/YourFirstJavaWebApp] threw exception [An exception occurred processing [/SignUp.jsp] at line [24]
21: a.setCountry(request.getParameter("country"));
22: String Skillset = "";
23: String Skills[] = request.getParameterValues("skill");
24: for (String i : Skills) {
25: Skillset = Skillset + i + " ";
26: }
27:
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.SignUp_jsp._jspService(SignUp_jsp.java:147)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:458)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/cs
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at dbconnect.DatabaseConfigure.dbconnect(DatabaseConfigure.java:14)
at org.apache.jsp.SignUp_jsp._jspService(SignUp_jsp.java:151)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:458)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)