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;
}
}