#java通过jdbc写sql语句向mysql插入数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
test(username, password);
}
public void test(String username, String password){
Connection connection;
PreparedStatement statement;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://192.168.184.100:3306/testsql?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC", "root", "123456");
String sql = "insert into test1 values(?,?)";
statement = connection.prepareStatement(sql);
statement.setString(1,username);
statement.setString(2, password);
statement.executeUpdate();
connection.close();
statement.close();
} catch (Exception e){
e.printStackTrace();
}
}