本人刚接触servlet 在用servlet进行数据库设计时出现了些问题,网页显示error:null
这是java代码
package sjk;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class simpleRegistration extends HttpServlet {
// Use a prepared statement to store a student into the database
private PreparedStatement pstmt;
/** Initialize global variables */
public void init() throws ServletException {
initializeJdbc();
}
/** Process the HTTP Post request */
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Obtain parameters from the client
String lastName = request.getParameter("lastName");
String firstName = request.getParameter("firstName");
String mi = request.getParameter("mi");
String phone = request.getParameter("telephone");
String email = request.getParameter("email");
String address = request.getParameter("street");
String city = request.getParameter("city");
String state = request.getParameter("state");
String zip = request.getParameter("zip");
try {
if (lastName.length() == 0 || firstName.length() == 0) {
out.println("Last Name and First Name are required");
return; // End the method
}
storeStudent(lastName, firstName, mi, phone, email, address,
city, state, zip);
out.println(firstName + " " + lastName +
" is now registered in the database");
}
catch(Exception ex) {
out.println("Error: " + ex.getMessage());
}
finally {
out.close(); // Close stream
}
}
/** Initialize database connection */
private void initializeJdbc() {
try {
// Declare driver and connection string
String driver = "com.mysql.jdbc.Driver";
String user="root"; //root改成你实际的数据库登录名
String passWord="1234"; //123 改为实际的密码
String connectionString = "jdbc:mysql://pc-win8/shiyan";
// For MySQL
// String driver = "com.mysql.jdbc.Driver";
// String connectionString = "jdbc:mysql://localhost/test";
// For Oracle
// String driver = "oracle.jdbc.driver.OracleDriver";
// String connectionString = "jdbc:oracle:" +
// "thin:scott/tiger@liang.armstrong.edu:1521:orcl";
// Load the driver
Class.forName(driver);
// Connect to the sample database
Connection conn = DriverManager.getConnection
(connectionString,user,passWord);
// Create a Statement
pstmt = conn.prepareStatement("insert into Address " +
"(lastName, firstName, mi, telephone, email, street, city, "
+ "state, zip) values (?, ?, ?, ?, ?, ?, ?, ?, ?)");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/** Store a student record to the database */
private void storeStudent(String lastName, String firstName,
String mi, String phone, String email, String address,
String city, String state, String zip) throws SQLException {
pstmt.setString(1, lastName);
pstmt.setString(2, firstName);
pstmt.setString(3, mi);
pstmt.setString(4, phone);
pstmt.setString(5, email);
pstmt.setString(6, address);
pstmt.setString(7, city);
pstmt.setString(8, state);
pstmt.setString(9, zip);
pstmt.executeUpdate();
}
}
网页的界面是酱紫的,不要嫌弃他简陋。。。菜鸟只是想尝试一下
填好之后点击submit就会显示error:null
根本不能把数据存到数据库里啊啊啊