我正在使用NetBeans 8.1 IDE,Apache Tomcat版本8.0.27和MySQL作为数据库。 端口号和数据库名称正确。但它不起作用。 此后不起作用 Connection cn = DriverManager.getConnection(“ jdbc:mysql:// localhost:.......”,“ root”,“ password”); 网页屏幕快照已链接。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class CheckSignUp extends HttpServlet {
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException{
PrintWriter pw=res.getWriter();
String name=req.getParameter("name");
String pass=req.getParameter("pass");
pw.write("<h1>Welcome</h1>");
try{
pw.write("<br><p>Please wait Creating Connection....<p>");
String sql="INSERT INTO user_demo VALUES(?,?);";
pw.write("<br><p>Created command is: <p>"+sql);
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
pw.write("<p>Driver Loaded</p>");
Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","root");
pw.write("<p>Connection Created</p>");[enter image description here][1]
PreparedStatement ps=cn.prepareStatement(sql);
pw.write("<p>Statement Created</p>");
ps.setString(1,name);
ps.setString(2, pass);
ps.executeUpdate();
cn.close();
pw.write("<br><p>New user added Sucessfully<p>");
}
catch(Exception ex){
pw.write("<br><p>Exception Occured: "+ex.getMessage());
}
}
}