//这是我的servlet代码。
import java.io.*;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author CSE
*/
public class AddStudent extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("Hello");
try
{
Class.forName("com.mysql.jdbc.Driver");//MySQL database connection
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javademo?"+"user=root&password=");
int rollno = Integer.parseInt(request.getParameter("txtrollno"));
String name = request.getParameter("txtname");
String department = request.getParameter("txtdept");
int mark1 = Integer.parseInt(request.getParameter("txtmark1"));
int mark2 = Integer.parseInt(request.getParameter("txtmark2"));
int mark3 = Integer.parseInt(request.getParameter("txtmark3"));
PreparedStatement ps=con.prepareStatement("insert into student(rollno,name,department,mark1,mark2,mark3)values(?,?,?,?,?,?)");
ps.setInt(1,rollno);
ps.setString(2,name);
ps.setString(3,department);
ps.setInt(4,mark1);
ps.setInt(5,mark2);
ps.setInt(6,mark3);
ps.executeUpdate();
con.commit();
ps.close();
con.close();
out.print(name);
out.print("\n Record inserted successfully");
}
catch (Exception e)
{
System.out.println("End");
}
}
}
答案 0 :(得分:0)
您似乎没有在此行中添加密码,未建立连接的可能性。
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/javademo?"+"user=root&password=");