喜從JSP應用程序java.lang.NumberFormatException:用JSP應用程序和MySQL
這裏試圖進入我的數據庫插入數據時,我發現了一個上面的錯誤是在JSP代碼
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Books database</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<br>
<div class="navigator">
<a id="currenttab" href="index.jsp">Add</a>
<a href="delete.jsp">Delete</a>
</div>
<%
String empfirstname = request.getParameter("empfirstname");
String empsurname = request.getParameter("empsurname");
String dpddept = request.getParameter("dpddept");
String extensionno = request.getParameter("extensionno");
String mobileno = request.getParameter("mobileno");
String emailaddress = request.getParameter("emailaddress");
String username = request.getParameter("username");
String password = request.getParameter("password");
if (empsurname != null && empfirstname != null
&& username != null && password != null) {
Users.Worker.Insert(empfirstname,empsurname,dpddept,extensionno,
mobileno,emailaddress,username,password);
}
%>
<br> <br> <br>
<form method='post' action='index.jsp'>
<table>
<tr><td>Please Enter your first name.</td>
<td><input type="text" id='empfirstname'></td></tr>
<tr><td>Please Enter your surname.</td>
<td><input type="text" id='empsurname'></td></tr>
<tr><td>Please Enter your Department.</td>
<td><input type="text" id='dpddept'></td></tr>
<tr><td>Please Enter your Extension Number.</td>
<td><input type="text" id='extensionno'></td></tr>
<tr><td>Please Enter your mobile Number.</td>
<td><input type="text" id='mobileno'></td></tr>
<tr><td>Please Enter your email Address.</td>
<td><input type="text" id='emailaddress'></td></tr>
<tr><td>Please Enter your email username.</td>
<td><input type="text" id='username'></td></tr>
<tr><td>Please Enter your email password.</td>
<td><input type="text" id='password'></td></tr>
<tr><td><input type="submit" name="submit"/></td></tr>
</table></form>
</body>
</html>
這是Java源代碼
package Users;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Worker {
static final String url = "jdbc:mysql://localhost:3306/users";
public static void Insert(String empfirstname,String empsurname,
String dpddept,String extensionno,String mobileno,
String emailaddress,String username,String password) {
try {
String insert = "INSERT INTO users(empfirstname,empsurname,dpddept,extensionno,"
+ "mobileno,emailaddress,username,password)" +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "dpd2014");
PreparedStatement ps = con.prepareStatement(insert);
ps.setString(1, empfirstname);
ps.setString(2, empsurname);
ps.setString(3, dpddept);
ps.setInt(4, Integer.parseInt(extensionno));
ps.setString(5, mobileno);
ps.setString(6, emailaddress);
ps.setString(7, username);
ps.setString(8, password);
ps.executeUpdate();
con.close();
} catch (Exception ex) {
Logger.getLogger(Worker.class.getName()).log(
Level.SEVERE, null, ex);
}
}
public static List GetUsers() {
List<String> list = new ArrayList<String>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "dpd2014");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM users");
while(result.next())
{
list.add(result.getString("empfirstname"));
list.add(result.getString("empsurname"));
list.add(result.getString("dpddept"));
list.add(result.getString("extensionno"));
list.add(result.getString("mobileno"));
list.add(result.getString("emailaddress"));
list.add(result.getString("username"));
list.add(result.getString("password"));
}
con.close();
} catch (Exception ex) {
Logger.getLogger(Worker.class.getName()).log(
Level.SEVERE, null, ex);
}
return list;
}
public static void Delete(String employeeno) {
try {
String delete = "DELETE from users WHERE employeeno = ?";
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, "root", "dpd2014");
PreparedStatement ps = con.prepareStatement(delete);
ps.setString(1, employeeno);
ps.executeUpdate();
con.close();
} catch (Exception ex) {
Logger.getLogger(Worker.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}
終於這是MySQL表
users
(Employeeno int(11) AI PK
empfirstname varchar(30)
empsurname varchar(40)
dpddept varchar(30)
extensionno int(11)
mobileno varchar(30)
emailaddress varchar(30)
username varchar(30)
password varchar(30))
我希望這是足夠的信息,任何幫助,將不勝感激
僅發佈相關代碼,哪一行顯示錯誤 – SpringLearner
您在哪一行代碼中獲得NumberFormatException。 – Gundamaiah
哪一行錯誤正在拋出? – Sumeet