<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/book";
Connection connection= DriverManager.getConnection(url,"root","root");
Statement stmt=connection.createStatement();
String sql="select*from user";
ResultSet rs=stmt.executeQuery(sql);
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
%>
- 运行结果
- 使用PreparedStatement执行sql语句
String sql="insert into user(username,password,gender)values(?,?,?)";
ResultSet rs=ps.executeQuery();
while (rs.next()){
out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
}
%>
<form action="home.jsp" method="post">
<input type="text" name="username">
<input type="text" name="password">
<button type="submit">提交</button>
- 运行结果