歷史:history我如何可以插入和JSP在德比戰中刪除數據庫中的價值?
問題是我無法從數據庫中刪除客戶。我可以創建一個新客戶,但是如果我留下空白的字段,它會在數據庫中創建一個空記錄,但我會創建一個標準。
的client.java:
public class client implements DatabaseConnection{
private static Connection conn = null;
private static void createConnection(){
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void closeConnection(){
if (conn != null){
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public List clientList(){
createConnection();
List list=new ArrayList();
try {
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM customer");
while(rs.next()){
list.add(rs.getString("CNAME"));
list.add(rs.getString("ADDRESS"));
list.add(rs.getString("PHONENUMBER"));
}
stmt.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
return list;
}
public void newClient(String name, String address, String phoneNumber)
throws SQLException{
PreparedStatement ps = null;
try {
createConnection();
String insert="INSERT INTO CUSTOMER(CNAME,ADDRESS, PHONENUMBER)
VALUES(?,?,?)";
ps=conn.prepareStatement(insert);
ps.setString(1, name);
ps.setString(2, address);
ps.setString(3, phoneNumber);
ps.executeUpdate();
ps.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
finally {
ps.close();
closeConnection();
}
}
public void deleteClient(String ID){
try {
createConnection();
String delete="DELETE FROM CUSTOMER WHERE ID=?";
PreparedStatement ps=conn.prepareStatement(delete);
ps.setString(1, ID);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
中的index.jsp:
<jsp:useBean id="client" class="database.client" scope="page" />
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="lightgrey">
<%
String ID=request.getParameter("ID");
String NAME=request.getParameter("CNAME");
String ADDRESS=request.getParameter("ADDRESS");
String PHONENUMBER=request.getParameter("PHONENUMBER");
%>
<form method="post" action="">
<table border="0" align="left">
<th colspan="2" align="center" style="color: brown">Field</th>
<tr>
<td>Name:</td>
<td><input type="text" name="CNAME" style="background-
color:beige"/></td>
</tr>
<tr>
<td>Address?</td>
<td><input type="text" name="ADDRESS" style="background-
color:beige"/></td>
</tr>
<tr>
<td>PhoneNumber:</td>
<td><input type="text" name="PHONENUMBER" style="background-
color:beige"/></td>
</tr>
<input type="submit" name="OK" onclick="
<%
if(NAME!=null && ADDRESS!=null && PHONENUMBER!=null){
client.newClient(NAME, ADDRESS, PHONENUMBER);
}
%>" value="OK"/>
<input type="submit" name="Cancel" onclick="
<%
//nothing
%>" value="Cancel"/>
<input type="submit" name="Delete" onclick="
<%client.deleteClient(ID);%>" value="Delete"/>
</table>
<table border="2">
<th colspan="4" align="center" bgcolor="orange">Clients</th>
<tr bgcolor="silver" align="center">
<td> </td>
<td>Name</td>
<td>Address</td>
<td>PhoneNumber</td>
</tr>
<%
List list=client.clientList();
Iterator it=list.iterator();
while(it.hasNext()){
out.print("<tr bgcolor='lightgreen'>");
out.print("<td>");
NAME=(String)it.next();
out.print("<input type='radio' name='ID' value="+NAME+"/>");
out.print("</td>");
out.print("<td>");
out.print(NAME);
out.print("</td>");
for (int i = 0; i < 2; i++) {
out.print("<td>");
out.print(it.next());
out.print("</td>");
}
out.print("</tr>");
}
%>
</table>
</form>
</body>
</html>
如果我離開空白字段,然後單擊確定出現以下錯誤消息:org.apache: 所致.derby.client.am.SqlException:沒有實際的連接。
如果沒有連接如何創建一個空的記錄?
和:
Severe: java.lang.NullPointerException at: client.java PreparedStatement ps=conn.prepareStatement(delete);
編輯 新的錯誤: 嚴重:java.sql.SQLDataException:整數無效的字符串格式。
有關,包括所有的方法訪問該數據庫的createConnection()和closeConnection()什麼? – RubioRic
插入的記錄問題是否已解決?剩餘的問題是刪除? – user75ponic
大概是的,只有問題是刪除。和一個小問題,插入到我可以插入空的記錄,不看,如果(NAME!= NULL &&地址!= NULL && PHONENUMBER!= NULL) – Lobi