gpt4 book ai didi

java - 我的 servlet 没有在 mysql 中保存数据

转载 作者:行者123 更新时间:2023-11-28 23:55:38 25 4
gpt4 key购买 nike

大家好,我正在学习 servlet,我在将数据保存到 mysql 时遇到问题,当用户输入值时它没有保存在 mysql 中。大家好,我正在学习 servlet,我在将数据保存到 mysql 时遇到问题,当用户输入值时它没有保存在 mysql 中。

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class NewServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();


String name = request.getParameter ("name");
String age=request.getParameter("age");

out.println("your name is " +name+" <br>");
out.println("Your age is " +age+"<br> ");
try {
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");

//creating connection with the database
Connection con=DriverManager.getConnection
("jdbc:mysql://localhost:3306/mysql","username=root","password=root");

PreparedStatement ps=con.prepareStatement
("insert into raa values(?,?)");

ps.setString(1, name);
ps.setString(2,age );

int i=ps.executeUpdate();

out.println("ACCOUNT REVIEW<br>");




if(i>0)
{
out.println("You are sucessfully registered");
}

}
catch(Exception se)
{
se.printStackTrace();

}
}
}

最佳答案

getConnection 不接受名称/值对中的数据库凭据。替换

 Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mysql","username=root","password=root");

 Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/mysql","root","root");

关于java - 我的 servlet 没有在 mysql 中保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com