我为代表创建Restful Web服务,以便从UI登录并通过JDBC连接到Mysql数据库。在Windows 10上使用动态Web服务-web模块版本2.5,Apache Tomcat服务器8.0.我在Java类中编写了Query并且连接到数据库。当运行tomcat服务器..它转到ui并且当单击提交按钮时,下一步操作是连接到DB但它的抛出:
UI和控制台中的Java.null.point.exception com.mysql.jdbc.CommunicationsException:通信链接失败 由于潜在的例外:**开始嵌入异常** java.io.EOFException的
public class loginservlet6_1 extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con=null;
Statement st=null;
ResultSet rs=null;
public void init(ServletConfig config)
{
try
{
System.out.println("Database connectionestablishment");
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:1002/root","root","root");
//(jdbc:mysql://localhost:portnum/dbname , username,password)
System.out.println("Database conn established");
}
catch(Exception e)
{
System.out.println(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
try
{
st = con.createStatement();
rs = st.executeQuery("select UserName,Password from reps where UserName='"+username+"' and Password='"+password+"'");
while(rs.next())
{
System.out.println("Username : "+rs.getString(1)+" Password : "+rs.getString(2));
if(username.equals(rs.getString(1)) && password.equals(rs.getString(2))){
JOptionPane.showMessageDialog(null,"Login Successful");
response.sendRedirect("/login.html");
}
else
{
JOptionPane.showMessageDialog(null,"Login Not Successful");
/*RequestDispatcher rd = request.getRequestDispatcher("/RegisterServlet");
rd.forward(request, response);
*/
response.sendRedirect("/TestDemo6/login.html");
}
}
}
catch(Exception e){
out.println(e);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
public void destroy()
{
try {
rs.close();
} catch (SQLException se) {
se.printStackTrace();
}
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}