以下のコードを実行すると、すべての挿入の前に、自動的に行が Null 値で埋められ、実際の値が挿入されます。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="style.css" type="text/css">
<title>Connecting to a Data base</title>
</head>
<body>
<%
String connectionURL="jdbc:mysql://localhost:3306/jsp_test";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "exeterblr");
if(!connection.isClosed()) {
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
}
else
{
out.println("Cannot connect to DB");
}
String name=request.getParameter("name");
String email=request.getParameter("email");
String phone=request.getParameter("phone");
PreparedStatement pstatement=null;
int updateTable=0;
String queryString = "INSERT INTO sample_info(name,email,phone) VALUES (?, ?, ?)";
pstatement=connection.prepareStatement(queryString);
pstatement.setString(1, name);
pstatement.setString(2, email);
pstatement.setString(3, phone);
updateTable=pstatement.executeUpdate();
%>
<form method="post" action="index.jsp">
<table>
<tr>
<td>Name</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>Email</td><td><input type="text" name="email"></td>
</tr>
<tr>
<td>Phone</td><td> <input type="text" name="phone"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</body>
値を挿入すると、データベースには次のものが含まれます。
Name Email Phone <br />
NULL NULL NULL <br />
ActualValue ActualValue Actual Value