大家好,我正在开发简单的 jsp 文件,该文件从学生那里获取一些信息,如姓名、性别、技能等,我将其定向到 servlet 并给她提供数据库连接。但是当我运行代码时,它没有连接到数据库。有人可以帮帮我吗。这是我的 servlet 代码
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con=null;
String url ="jdbc:mysql://localhost:3306/";
String dbName="student_db";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String password = "admin";
String bttn_value = request.getParameter("submit");
if(bttn_value.equalsIgnoreCase("submit"))
{
String name = request.getParameter("fname");
String gender = request.getParameter("gender");
String skill ="";
System.out.println("2222222222");
String skills[]= request.getParameterValues("skill");
for(int i=0;i<skills.length;i++)
{
skill +=skills[i]+",";
}
String addr=request.getParameter("adname");
String country = request.getParameter("country");
Statement stmt;
try
{
System.out.println("444444444444");
Class.forName(driver).newInstance();
System.out.println("333333333");
con = DriverManager.getConnection(url + dbName, username,
password);
System.out.print("Connected to database");
String query1= "insert into student values name='"
+name +"',gender='"+gender+ "',skills='"+skill + "',addname='"
+addr +"',country='" +country +"'";
stmt =con.createStatement();
stmt.executeUpdate(query1);
out.print("<br>");
response.sendRedirect("std.jsp");
con.close();
System.out.print("Disconnected from DB");
}
catch (Exception e)
{
out.println(e.getMessage());
System.out.println("123456");
}
这是我的控制台
和我得到的输出
预先感谢您,任何帮助都是可观的。