我不确定这里出了什么问题。我编写了类似的项目,但工作得很好但无论出于何种原因,数据都没有写入数据库。
我目前试图实现的功能是从注册表单获取信息并将其存储在我的phpmyadmin数据库中。
这个代码与另一个人做这个项目是相同的(逐字逐句),他们的工作正常。让我相信问题在于以某种方式使用JDBC驱动程序或数据库。
这是表单页面代码
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Phone Auction Site</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Login</h1>
<form action="login.jsp">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="password"><br>
<input type="submit" value="Log In">
</form>
<h1>Sign Up</h1>
<form action="signup.jsp">
Username:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="password"><br>
First Name:<br>
<input type="text" name="firstName"><br>
Last Name:<br>
<input type="text" name="lastName"><br>
Address:<br>
<input type="text" name="address"><br>
Phone Number:<br>
<input type="text" name="phoneNum"><br>
Email:<br>
<input type="text" name="email"><br>
<input type="submit" value="Sign Up">
</form>
</body>
</html>
这是signup.jsp页面,它获取属性,创建实例并调用saveToDatabase方法
<%@page import="phoneauctionronan.Customer"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sign Up</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String address = request.getParameter("address");
String phoneNum = request.getParameter("phoneNum");
String email = request.getParameter("email");
Customer c = new Customer(username, password, firstName, lastName, address, phoneNum, email);
c.saveToDatabase();
%>
</body>
</html>
这是Customer类
package phoneauctionronan;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Customer
{
private int id;
private String username;
private String password;
private String firstName;
private String lastName;
private String address;
private String phoneNum;
private String email;
public Customer()
{
}
public Customer(String username, String password, String firstName, String lastName, String address, String phoneNum, String email)
{
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phoneNum = phoneNum;
this.email = email;
}
public boolean saveToDatabase()
{
boolean saved = false;
Connection c = DBHelperClass.getConnection();
if (c != null)
{
try
{
Statement s = c.createStatement();
String template = "INSERT INTO customer (username, password, firstName, lastName, address, phoneNumber, emailAddress) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement inserter = c.prepareStatement(template);
inserter.setString(1, this.username);
inserter.setString(2, this.password);
inserter.setString(3, this.firstName);
inserter.setString(4, this.lastName);
inserter.setString(5, this.address);
inserter.setString(6, this.phoneNum);
inserter.setString(7, this.email);
inserter.executeUpdate();
saved = true;
}
catch (SQLException ex)
{
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
return saved;
}
public Customer validateCustomer(String username, String password)
{
boolean found = false;
Connection c = DBHelperClass.getConnection();
if(c!=null)
{
try
{
Statement s = c.createStatement();
String template = "SELECT * from customers where username = ? and password = ?";
PreparedStatement inserter = c.prepareStatement(template);
inserter.setString(1, username);
inserter.setString(2, password);
ResultSet resultSet = inserter.executeQuery();
while(resultSet.next())
{
this.id = resultSet.getInt("userID");
this.username = resultSet.getString("userName");
this.password = resultSet.getString("password");
this.firstName = resultSet.getString("firstName");
this.lastName = resultSet.getString("lastName");
this.address = resultSet.getString("address");
this.phoneNum = resultSet.getString("phoneNum");
this.email = resultSet.getString("email");
found = true;
}
}
catch (SQLException ex)
{
Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
}
}
return this;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getPhoneNum()
{
return phoneNum;
}
public void setPhoneNum(String phoneNum)
{
this.phoneNum = phoneNum;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
最后这里是DBConnection类
package phoneauctionronan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DBHelperClass
{
public static Connection getConnection()
{
String host = "localhost";
String dbName = "phones";
int port = 3306;
String mySqlUrl = "jdbc:mysql://" + host + ":" + port
+ "/" + dbName;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println("ClassNotFound !!!!" + e);
}
Properties userInfo = new Properties();
userInfo.put("user", "root");
userInfo.put("password", "password");
try
{
Connection connection = DriverManager.getConnection(mySqlUrl, userInfo);
return connection;
}
catch (SQLException ex)
{
Logger.getLogger(DBHelperClass.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Here is a pic of my customer table structure
Here a pic of the name of the db and table just in case
完全失去了导致这种情况的原因!