通过JDBC连接数据库H2

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
    .form{
    position: absolute;
    padding: 60px;
    top: 200px;
    left: 550px;
    text-align:center;
    width: 400px;
    height: 200px;
    border: 1px solid;
    }
</style>
</head>
<body>
<div class="form" >
   <form method="post" action="grade.jsp">
      分数>=<input type="text"  name="selec" style="width: 200px; height: 28px">
      <input type="submit" value="查询" id="search1" height="50px"><br><br>
        <div style="left: 180px ;position: absolute" >
        <table border="1" >
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>成绩</th>
            </tr>
            <%!int time=0; %>
    <%
    try{
        Class.forName("org.h2.Driver");
        String url="jdbc:h2:D:/temp/h2/mydb";
        String username="sa";
        String password="";
        Connection conn=DriverManager.getConnection(url,username,password);
        //String s=request.getParameter("selec");
        //System.out.print("+"+s);
        int grade=0;
        if(time==0){
            
        }else{
            grade=Integer.parseInt(request.getParameter("selec"));
        }
        time++;
        String sql="select * from STUDENT WHERE SCORE>=?";
        PreparedStatement stmt=conn.prepareStatement(sql);
        stmt.setInt(1, grade);
        ResultSet rs=stmt.executeQuery();
        %>
            <% while(rs.next()){
    %>
            <tr>
                <td><%=rs.getString("NO") %></td>
                <td><%=rs.getString("NAME") %></td>
                <th><%=rs.getInt("SCORE") %></th>
            </tr>
            <% } %>
        </table>
      </div>
  </form>
</div>
    <%
    rs.close();
        stmt.close();
        conn.close();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        } catch(SQLException e){
            e.printStackTrace();
        }
        %>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40384579/article/details/84471262