java开发,利用servlet实现登录及数据库查询

  • F4_646123
    了解作者
  • 749.9KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-25 05:35
    上传日期
java开发,利用servlet实现登录及数据库查询
Login.zip
内容介绍
package com.oa.test; import java.util.ArrayList; import java.util.List; import java.sql.*; public class StudentDao { public List<StudenModel> getAll(){ List<StudenModel> students = new ArrayList<StudenModel>(); Connection conn = null; java.sql.PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/companyinfoitem?user=root&password=root"; String user = "root"; String password = "root"; String sql = "select * from student"; conn = DriverManager.getConnection(url, user, password); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ int id = rs.getInt(1); String studentName = rs.getString(2); String location = rs.getString(3); String telephone = rs.getString(4); StudenModel student = new StudenModel(id, studentName, location, telephone); students.add(student); } }catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(conn != null) conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return students; } }
评论
    相关推荐