问题
I created a form in a JSP to insert data to a database in derby but it doesn't work.
The database name is CUSTOMER. The tables:
ID (int), CNAME (varchar), ADDRESS (varchar), PHONENUMBER (varchar)
The content of client.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<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">
<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>TelNumber</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"/>
</table>
</form>
</body>
The content of client.java.
package database;
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 void newClient(String name, String address, String phoneNumber){
try {
createConnection();
String insert="INSERT INTO CUSTOMER(CNAME,ADDRESS, PHONENUMBER) VALUES(?,?,?)";
PreparedStatement ps=conn.prepareStatement(insert);
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, phoneNumber);
ps.executeUpdate();
ps.close();
} catch (Exception e) {
}
}
}
The content of databaseConnection.
package database;
public interface DatabaseConnection{
String URL="jdbc:derby://localhost:1527/Test";
String USER="myusername";
String PASSWORD="mypassword";
}
EDIT
The error message:
Caused by: org.apache.derby.client.am.SqlException: Column position '4' is
outside the allowed range. The columns of ResultSet are '3'.
Org.apache.derby.client.am.ColumnMetaData.checkForValidColumnIndex (unknown
source)
EDIT2
public void deleteClient(String ID){
try {
String delete="DELETE FROM CUSTOMER WHERE ID=?";
PreparedStatement ps=conn.prepareStatement(delete);
ps.setString(1, ID);
ps.executeUpdate();
} catch (Exception e) {
}
}
回答1:
Your preparedStatement index should start from 1, not from 2, so try as
public void newClient(String name, String address, String phoneNumber){
PreparedStatement ps = null;
try {
createConnection();
String insert="INSERT INTO CUSTOMER(ID,CNAME,ADDRESS, PHONENUMBER) VALUES(?,?,?,?)";
ps=conn.prepareStatement(insert);
ps.setString(1, name);
ps.setString(2, address);
ps.setString(3, phoneNumber);
ps.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
ps.close();
closeConnection();
}
}
PreparedStatement or Connection close methods should be in the finally part of try catch finally
block.
Similar to the closeConnection()
method, you can create another method for closing preparedStatement and call that in the finally block.
Update 1
If you are capturing Id in Java, then try as
public void newClient(Integer id, String name, String address, String phoneNumber){
PreparedStatement ps = null;
try {
createConnection();
String insert="INSERT INTO CUSTOMER(CNAME,ADDRESS, PHONENUMBER) VALUES(?,?,?)";
ps=conn.prepareStatement(insert);
ps.setInt(1, id)
ps.setString(2, name);
ps.setString(3, address);
ps.setString(4, phoneNumber);
ps.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
ps.close();
closeConnection();
}
}
And in the JSP, modify as
if(ID!=null && NAME!=null && ADDRESS!=null && PHONENUMBER!=null){
client.newClient(ID, NAME, ADDRESS, PHONENUMBER);
}
来源:https://stackoverflow.com/questions/44149892/how-can-i-insert-value-to-a-database-to-derby-in-jsp