jsp连接操作数据库的方法可能有很多,今天给大家介绍的是Statement方法
步骤分为以下几步:

1. 加载数据库连接驱动

 Class.forName("com.mysql.jdbc.Driver"); 

2. 获取数据库连接

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschol", "root", "password"); 

3. 获取Statement对象,执行sql语句

String sql = "select * from `student`"; // 执行sql语句
Statement st =conn.createStatement();
ResultSet rs=st.executeQuery(sql);//获取数据集合

4. 遍历,处理sql执行结果

while(rs.next()){
    int studentId = rs.getInt("StudentNo");
    String studentName = rs.getString("StudentName");
    String addRESS = rs.getString("AddRESS");
    String email = rs.getString("Email");
    System.out.println("studentId:"+studentId+"studentName:"+studentName+"addRESS:"+addRESS+"email"+email);
}

5. 关闭流

rs.close();
st.close();
conn.close();

全部代码参考下面:

public static void main(String[] args) {
    Connection conn =null;
    Statement st = null;
    ResultSet rs=null;

    try {
        // 1. 加载数据库连接驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2. 获取数据库连接
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschol", "root", "password");
        // 3. 获取Statement对象,执行sql语句
        String sql = "select * from `student`"; // 执行sql语句
        st =conn.createStatement();
        rs=st.executeQuery(sql);//获取数据集合
        // 4. 遍历,处理sql执行结果
        while(rs.next()){
            int studentId = rs.getInt("StudentNo");
            String studentName = rs.getString("StudentName");
            String addRESS = rs.getString("AddRESS");
            String email = rs.getString("Email");
            System.out.println("studentId:"+studentId+"studentName:"+studentName+"addRESS:"+addRESS+"email"+email);
        }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally{
        try {
            // 5. 关闭流
            rs.close();
            st.close();
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

当然,使用Statement这种方法还有很多弊端,对于初学者,我们可以用以学习!!!