0

I have a JSP file that gets forms from an HTML file and sends them to an SQL database. here are the files:

attempt.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="attempt.jsp" method="post">
  <input type="text" name="fname"></input> name
  <input type="text" name="age"></input> age
  <input type="submit"></input>
 </form>
</body>
</html>

attempt.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <%
  Class.forName("com.mysql.jdbc.Driver").newInstance();
  Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/DB","root","060901");
  PreparedStatement st = con.prepareStatement("insert into tb values(?,?);");
  st.setString(1,request.getParameter("fname"));
  int a = Integer.parseInt(request.getParameter("age"));
  st.setInt(2,a);
  int num = st.executeUpdate();
  ResultSet res = st.executeQuery("select * from tb;");
  res.beforeFirst();
 %> 
 <table>
 <%
  while(res.next()) {
 %>
  <tr>
   <td><%=res.getString("name") %></td>
   <td><%=res.getInt("age") %></td>
  </tr>
 <%  
  }
 %>
 </table>
</body>
</html>

I am quite new to the subject of servers and databases, so I have a hard time cracking this one. The MySQL-connector jar is already in the buildpath and I have already tried importing it to the JSP page. Does anyone know what to do?

Thanks in advance.

  • To have the MySQL driver in the build path is not enough, it has to be deployed either in `WEB-INF/lib` folder of your application, or in Tomcat's `lib` folder. – Jozef Chocholacek May 31 '17 at 07:00
  • it is in my tomcat lib folder, and I will now try to add it to WEB-INF/lib folder UPDATE: it worked! thanks a lot! – Ron Leibovich May 31 '17 at 07:27

0 Answers0