我的讲师试图但无法帮助我:使用XAMPP,为什么我无法将HTML / JSP链接到mySQL数据库?

时间:2019-03-06 13:42:57

标签: java json jsp tomcat xampp

我现在正在上课,甚至我的讲师也无法帮助我将JSP链接到mySQL数据库。他本人不确定我哪里出了问题。我们甚至下载了Connector / J 8.0.15,如果它未链接的Tomcat甚至没有通过,也可以尝试一下。

<!doctype jsp>

<html>
  <body>
    <form method="post" action="lesson-2.jsp">
      First Name: <br>
      <input type="type" name="firstname" value=""><br><br>
      Last Name: <br>
      <input type="text" name="lastname" value=""><br><br>
      Gender: <br>
      <input type="text" name="gender" value="">
      <input type="text" name="address" value="">
      <input type="text" name="phone" value="">
      <input type="text" name="course" value="">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

<%@ page import = "java.util.*" %>
<%@ page import = "java.sql.*" %>

<%
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
//jdbc = Java database connectivity
//most of the time have to read manual for database for Java
//first part of : is jdbc i.e. Java Database connectivity
//mysqpl = which database we are using
//localhost = host name and port number
//
String connectionURL = "jdbc:mysql://localhost:3306/abc";
Connection connection = null;
PreparedStatement pstatement = null;


try {
//Check for library
Class.forName("com.mysql.jdbc.Driver");
//Username and password to database
connection = DriverManager.getConnection(connectionURL,"root","");
//insert data into the database
String queryString = "insert into student_master(firstname,lastname) values(?,?)";


pstatement = connection.prepareStatement(queryString);

pstatement.setString(1, firstname);
pstatement.setString(2, lastname);

//execute command to the database
int updateQuery = pstatement.executeUpdate();

out.println(updateQuery);
out.println(firstname);
out.println(lastname);

}
catch (Exception ex)
{
  out.println("Unable to connect to database.");
}
finally
{
  pstatement.close();
  connection.close();
}

//C:\xampp\tomcat\webapps\JavaProject (Lithan)\lesson-1.jsp
%>

0 个答案:

没有答案