Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Student getStudent(int id) {
- Student stu = null;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection mySQLCon = DriverManager.getConnection("jdbc:mysql://localhost/company", "root", "1q2w3e");
- PreparedStatement ps =
- mySQLCon.prepareStatement("select * from student where id=?");
- ps.setInt(1, id);
- ResultSet rs = ps.executeQuery();
- rs.next();
- return new Student(rs.getInt("id"), rs.getString("name"), rs.getString("lastname"));
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public List<Student> getAllStudents() {
- List<Student> stus = new ArrayList<>();
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection mySQLCon = DriverManager.getConnection("jdbc:mysql://localhost/company", "root", "1q2w3e");
- PreparedStatement ps =
- mySQLCon.prepareStatement("select * from student");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- Student s = new Student(rs.getInt("id"), rs.getString("name"), rs.getString("lastname"));
- stus.add(s);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return stus;
- }
- public boolean addStudent(String name, String lastname) {
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection mySQLCon = DriverManager.getConnection("jdbc:mysql://localhost/company", "root", "1q2w3e");
- PreparedStatement ps =
- mySQLCon.prepareStatement("insert into student (name,lastname) values (?,?)");
- ps.setString(1, name);
- ps.setString(2, lastname);
- int result = ps.executeUpdate();
- if(result ==1)
- {
- return true;
- }
- else{
- return false;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement