First, create a new home page, index,jsp file
Then write the code for the index.jsp file:
<%
First, get the parameters passed by the client, (add the first few)
This example: Ignore first (but this thought step is certain to exist)
Deal with related business logic, why do you do it?
The logic of this example is to take out all the data
-->> Accessing the database
String username_str=request.getparameter ("Txtquery");
%>
<form action= "index.jsp" method= "POST" >
Name: <input type= "text" name= "Txtquery" value= "<%=username_str%>" >
<input type= "Submit" value= "inquiry" >
</form>
<br><br><br>
<table border= "1" width= "80%" align= "Center" >
<tr>
<th> Serial Number </th>
<th> name </th>
<th> Sex </th>
<th> Date of birth </th>
<th> Operations </th>
</tr>
<%
Take a look at the name of the query
Five key parameters for connecting to a database
String Driverclass = "Com.mysql.cj.jdbc.Driver";
String ServerIP = "localhost";//server IP address
String databaseName = "OA";//which database is connected to
String username = "root";//user name
String password = "123456";//Password
String jdbcurl= "jdbc:mysql://" +serverip+ ": 3306/" +databasename+ "? Servertimezone=asia/shanghai&usessl=true";
Class.forName (Driverclass);
Connection conn=drivermanager.getconnection (Jdbcurl,username,password);
If empty, represents the current state is not a query, but instead displays all the content
PreparedStatement PST;
if (username_str==null) {
PST = conn.preparestatement ("SELECT * from student");
}else{
PST = conn.preparestatement ("SELECT * from student where user_name like?");
Pst.setstring (1, "%" +username_str+ "%");
}
ResultSet Rs=pst.executequery ();
is essentially not saving any data
ResultSet a variable that points to a database that itself does not contain content
Pointing to where? Point to the head of the dataset (not the first record)
int i=0;
while (Rs.next ()) {
%>
<TR align= "center" >
<td><%out.print (++i);%></td>
<td><%=rs.getstring ("user_name")%></td>
<td><%=rs.getstring ("Sex")%></td>
<td><%=rs.getstring ("Brit")%></td>
<td><a href= "select.jsp?id=<%=rs.getstring (" id ")%>" > Edit </a>
<a href= "delete.jsp?id=<%=rs.getstring (" id ")%>" > Delete </a>
</td>
</tr>
<%
}
Rs.close ();
Pst.close ();
Conn.close ();
%>
</table>
<form action= "add.jsp" >
<input type = "Submit" value= "Add" >
</form>
To remember to add java.sql.* references
The above code is already included in the search function, you can read it yourself.
Then the next implementation adds functionality, and a button has been added to the index interface
Then we'll write the Insert code:
<%
String name=request.getparameter ("txtname");
String sex=request.getparameter ("sex");
String birt=request.getparameter ("Txtbirthday");
Five key parameters for connecting to a database
String Driverclass = "Com.mysql.cj.jdbc.Driver";
String ServerIP = "localhost";//server IP address
String databaseName = "OA";//which database is connected to
String username = "root";//user name
String password = "123456";//Password
Pieced together into a full URL address
String Jdbcurl = "jdbc:mysql://" +serverip+ ": 3306/" +databasename+ "? Servertimezone=asia/shanghai&usessl=true"; /each database is different
Class.forName (Driverclass);
Connection conn=drivermanager.getconnection (Jdbcurl,username,password);
String sql= "INSERT into student (User_name,sex,brit) VALUES (?,?,?)";
PreparedStatement ps=conn.preparestatement (SQL);
Ps.setstring (1, name);
Ps.setstring (2, sex);
Ps.setstring (3, Birt);
Ps.execute ();
Ps.close ();
Conn.close ();
Response.sendredirect ("index.jsp");
%>
JSP additions and deletions to change the project