JSP连接SQL数据库实现查找(支持模糊查找,查找年龄段),插入信息<实例>
本代码适合JSP初学者!转载请注明出处谢谢! ====Search.html============================================================ <html> <head> <title>学生信息查询</title> <META http-equiv="Content-Type" content="text/html; charset=GB2312"> </head> <!--Vmer设计,QQ:6466909.转载请注明出处.--> <body> <center> <h2>学生信息查询</h2> <br /> <br /> <form method="POST" action="Name.jsp"> <h4>按姓名查找(支持模糊查询)</h4> <table bgcolor="#CCCCCC"> <tr> <td>查找姓名</td> <td><input type="text" name="name" size="15" /></td> <td><input type="submit" value="查找"></td> </tr> </table> </form> <br/> <br /> <form method="POST" action="Age.jsp"> <h4>按年龄查找</h4> <table border="1" bgcolor="#CCCCCC"> <tr> <td>查找年龄</td> <td><input type="text" name="agemin" size="5" /></td> <td>到</td> <td><input type="text" name="agemax" size="5" /></td> <td><input type="submit" value="查找"></td> </tr> </table> </form> <br /> <br /> <form action="Insert.jsp" method="POST"> <h4>插入信息到表中</h4> <table border="1" bgcolor="#cccccc"> <tr> <td>姓名</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>性别</td> <td><input type="text" name="sex" /></td> </tr> <tr> <td>年龄</td> <td><input type="text" name="age" /></td> </tr> <tr> <td>系别</td> <td><input type="text" name="dept" /></td> </tr> <tr> <td><input type="submit" value="插入" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </center> </body> </html> ======================================================================== ====Name.jsp============================================================ <%@ page contentType="text/html; charset=GBK" %> <%@ page import="java.sql.*" %> <html> <head> <title></title> <META http-equiv="Content-Type" content="text/html; charset=GBK"> </head> <body> <% String name=request.getParameter("name"); if(name==null) { name=""; } byte b[]=name.getBytes("ISO-8859-1"); name=new String(b); Connection con; Statement sql; ResultSet rs; try {//建立JDBC-ODBC桥驱动程序,用到java.lang包中的类Class,调用其方法forName(). Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} try {//创建连接对象con,它属于Connection类,然后用类DriverManager调用getConnection(). con=DriverManager.getConnection("jdbc:odbc:stu"); sql=con.createStatement();//创建SQL语句对象. rs=sql.executeQuery("SELECT* FROM stuInfo WHERE stuName LIKE '%"+name+"%'");//执行SQL语句并返回结果. %> <center> <h2>按姓名查找的学生信息</h2> <br /> <br /> <h4>输出姓名为<%=name%>的学生信息:</h4> <table border="1" bgcolor="#cccccc"><!--设置用以表格显示.--> <tr> <th width="50">学号</th> <th width="50">姓名</th> <th width="50">性别</th> <th width="50">年龄</th> <th width="50">系别</th> </tr> <% while(rs.next())//顺序取出结果集中的数据. {%> <tr> <td><%=rs.getString(1)%></td> <!---取出表中第一个字段--> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getInt(4)%></td> <td><%=rs.getString(5)%></td> </tr> <%}%> </table> </center> <% con.close(); } catch(SQLException e1){} %> </body> </html> ============================================================================ =====Age.jsp================================================================ <%@ page contentType="text/html; charset=GBK" %> <%@ page import="java.sql.*" %> <html> <head> <title>按姓名查找的学生信息</title> </head> <body> <% String agemin=request.getParameter("agemin"); if(agemin==null) { agemin="0"; } String agemax=request.getParameter("agemax"); if(agemax==null) { agemax="100"; } Connection con; Statement sql; ResultSet rs; try {//建立JDBC-ODBC桥驱动程序,用到java.lang包中的类Class,调用其方法forName(). Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} try {//创建连接对象con,它属于Connection类,然后用类DriverManager调用getConnection(). con=DriverManager.getConnection("jdbc:odbc:stu"); sql=con.createStatement();//创建SQL语句对象. rs=sql.executeQuery("SELECT* FROM stuInfo WHERE stuAge<="+agemax+" AND stuAge>="+agemin+"");;//执行SQL语句并返回结果. %> <center> <h2>按年龄查找的学生信息</h2> <br /> <br /> <h4>输出年龄在<%=agemin%>与<%=agemax%>之间的学生信息:</h4> <table border="1" bgcolor="#cccccc"><!--设置用以表格显示.--> <tr> <th width="50">学号</th> <th width="50">姓名</th> <th width="50">性别</th> <th width="50">年龄</th> <th width="50">系别</th> </tr> <% while(rs.next())//顺序取出结果集中的数据. {%> <tr> <td><%=rs.getString(1)%></td> <!---取出表中第一个字段--> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getInt(4)%></td> <td><%=rs.getString(5)%></td> </tr> <%}%> </table> </center> <% con.close(); } catch(SQLException e1){} %> </body> </html> ============================================================================ ======Insert.jsp====================================================================== <%@ page contentType="text/html; charset=GB2312" %> <%@ page import="java.sql.*" %> <html> <head> <title></title> <META http-equiv="Content-Type" content="text/html; charset=GB2312"> </head> <body> <% String name=request.getParameter("name"); if(name==null) { name=""; } byte b[]=name.getBytes("ISO-8859-1"); name=new String(b); String sex=request.getParameter("sex"); if(sex==null) { sex=""; } byte s[]=sex.getBytes("ISO-8859-1"); sex=new String(s); String age=request.getParameter("age"); String dept=request.getParameter("dept"); if(dept==null) { dept=""; } byte d[]=dept.getBytes("ISO-8859-1"); dept=new String(d); Connection con; Statement sql; ResultSet rs; Statement stmt; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException e){} try { con=DriverManager.getConnection("jdbc:odbc:stu"); sql=con.createStatement();//创建SQL语句对象. stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate("INSERT INTO stuInfo VALUES ('"+name+"','"+sex+"','"+age+"','"+dept+"')"); rs=sql.executeQuery("SELECT* FROM stuInfo"); %> <center> <h2>学生信息表</h2> <br /> <br /> <h4>输出插入后的学生信息:</h4> <table border="1" bgcolor="#cccccc"><!--设置用以表格显示.--> <tr> <th width="50">学号</th> <th width="50">姓名</th> <th width="50">性别</th> <th width="50">年龄</th> <th width="50">系别</th> </tr> <% while(rs.next())//顺序取出结果集中的数据. {%> <tr> <td><%=rs.getString(1)%></td> <!---取出表中第一个字段--> <td><%=rs.getString(2)%></td> <td><%=rs.getString(3)%></td> <td><%=rs.getInt(4)%></td> <td><%=rs.getString(5)%></td> </tr> <%}%> </table> </center> <% con.close(); } catch(SQLException e1) { System.out.println(e1); } %> </body> </html> |
JSP连接SQL数据库实现查找/插入信息[参考]
最新推荐文章于 2024-08-19 20:48:55 发布