正在加载课程com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver'。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。
java.sql.SQLException:无法使用executeQuery()发出数据操作语句。
我可以在数据库中添加数据,但我无法更新数据。我该如何修复这个错误。这是我第一次做java项目。我无法解决此错误。
<form name="myForm" action= "addServlet" method="post">
<table id="table1" >
<tr>
<th><b>Email </b></th>
<th><input id="text1" type="text" name="email" ></th>
</tr>
<tr>
<th><b>Email Password </b></th>
<th><input id="text1" type="text" name="ePassword" ></th>
</tr>
<tr>
<th><b>Name </b></th>
<th><input id="text1" type="text" name="name" ></th>
</tr>
<tr>
<th><b>Country </b></th>
<th><input id="text1" type="text" name="country" ></th>
</tr>
<tr>
<th><b>Location </b></th>
<th><input id="text1" type="text" name="location" ></th>
</tr>
<tr>
<th>Gender</th>
<th><input type="radio" name="gender" value="male"checked="checked" tabindex="1" /> Male<input type="radio" name="gender" value="female"tabindex="2" /> Female</th>
</tr>
</table>
<table style="margin-left:700px">
<tr>
<th><button style="margin-right:50px" name="action" type="Reset" VALUE="Reset">Reset</th>
<th><button style="margin-right:50px" name="action" type="Add" VALUE="AddServlet">Add</button></th>
<th><button style="margin-right:50px" name="action" type="Update" VALUE="UpdateServlet">Update</button></th>
</tr>
</table>
</from>
public class Update {
public static boolean connectUserTable(String email,String name,String ePassword,String location,String country,String gender)throws SQLException, ClassNotFoundException{
boolean status=false;
try{
ResultSet resultSet = null;
dbConnection db = new dbConnection();
Connection conn = db.getConnection();
PreparedStatement st = conn.prepareStatement("UPDATE user set ePassword=?, name=?, location=?, country=?, gender=? WHERE email=?");
st.setString(1, email);
st.setString(2, ePassword);
st.setString(3, name);
st.setString(4, location);
st.setString(5, country);
st.setString(6, gender);
resultSet = st.executeQuery();
while(resultSet.next()){
resultSet.getString("email");
resultSet.getString("ePassword");
resultSet.getString("name");
resultSet.getString("location");
resultSet.getString("country");
resultSet.getString("gender");
}
ResultSet rs=st.executeQuery();
status=rs.next();
}catch(Exception e){
System.out.println(e);
}
return status;
}
}
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public addServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
response.setContentType("text/html");
String email= request.getParameter("email");
String ePassword = request.getParameter("ePassword");
String name = request.getParameter("name");
String location = request.getParameter("location");
String country = request.getParameter("country");
String gender = request.getParameter("gender");
String action = request.getParameter("action");
if("AddServlet".equals(action)){
dbConnection db = new dbConnection();
Connection conn = db.getConnection();
Statement st = conn.createStatement();
st.executeUpdate("Insert into user (email,ePassword,name,location,country,gender) values('"+email+"','"+ePassword+"','"+name+"','"+location+"','"+country+"','"+gender+"')");
response.sendRedirect("MainPage.jsp");
}
if("UpdateServlet".equals(action)){
response.setContentType("text/html");
if(Update.connectUserTable(email, name, ePassword, location, country, gender)){
RequestDispatcher rd=request.getRequestDispatcher("MainPage.jsp");
rd.include(request,response);
}
else{
RequestDispatcher rd=request.getRequestDispatcher("UserRegister.jsp");
rd.include(request,response);
}
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
out.close();
}
}
public class dbConnection {
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/oop?useSSL=false","root","");
return conn;
}catch(ClassNotFoundException e){
e.printStackTrace();
return null;
}catch(SQLException e){
e.printStackTrace();
return null;
}
}
}