Advertisement
Guest User

JOEP

a guest
Sep 21st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package dal;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.ArrayList;
  9.  
  10. import model.Student;
  11.  
  12. public class Dal {
  13.  
  14.  
  15.  
  16. private PreparedStatement stmt;
  17. private Connection conn;
  18.  
  19. public Connection getConnection() throws SQLException {
  20. try {
  21. DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
  22.  
  23. Connection conn = DriverManager.getConnection(
  24. "jdbc:sqlserver://localhost:1433;" + "databaseName=uppgift1;user=sa;password=stest123;");
  25. return conn;
  26. } catch (Exception e) {
  27. System.out.println(e);
  28. }
  29.  
  30. return null;
  31. }
  32.  
  33. public Student getStudentByName(String name) {
  34.  
  35. Student student = null;
  36.  
  37. try {
  38. conn = getConnection();
  39.  
  40. String sql = "select * from student where sname = ?";
  41.  
  42. stmt = conn.prepareStatement(sql);
  43.  
  44. stmt.setString(1, name);
  45.  
  46. ResultSet rs = stmt.executeQuery();
  47.  
  48. while (rs.next()) {
  49. String spnr = rs.getString("spnr");
  50. String sname = rs.getString("sname");
  51. String sadress = rs.getString("sadress");
  52. String stel = rs.getString("stel");
  53.  
  54. student = new Student(spnr, sname, sadress, stel);
  55.  
  56. }
  57. stmt.close();
  58. conn.close();
  59. } catch (Exception e) {
  60. System.out.println(e);
  61. }
  62.  
  63. return student;
  64. }
  65.  
  66. public ArrayList<Student> getAllStudents() {
  67.  
  68. ArrayList<Student> students = new ArrayList<Student>();
  69. try {
  70. conn = getConnection();
  71. String sql = "select * from student";
  72.  
  73. stmt = conn.prepareStatement(sql);
  74.  
  75. ResultSet rs = stmt.executeQuery();
  76.  
  77. while (rs.next()) {
  78. String spnr = rs.getString("spnr");
  79. String sname = rs.getString("sname");
  80. String sadress = rs.getString("sadress");
  81. String stel = rs.getString("stel");
  82.  
  83. Student student = new Student(spnr, sname, sadress, stel);
  84. students.add(student);
  85.  
  86. }
  87.  
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. return students;
  92. }
  93.  
  94. }
  95. // try {
  96. // // Ladda Oracle JDBC driver
  97. // DriverManager.registerDriver(new
  98. // com.microsoft.sqlserver.jdbc.SQLServerDriver());
  99. // } catch (Exception e) {
  100. // System.out.println("Kan inte database driver class: " + e);
  101. // }
  102. // try {
  103. // // syntax : <host>:<port>:<sid>.
  104. // Connection conn = DriverManager.getConnection(
  105. // "jdbc:sqlserver://localhost:1433;" +
  106. // "databaseName=uppgift1;user=sa;password=stest123;");
  107. // if (conn == null)
  108. // System.out.println("No driver found!");
  109. // else
  110. // System.out.println("Connection Established - SUCCESS!");
  111. // Statement stmt = conn.createStatement();
  112. // ResultSet rs = stmt.executeQuery("SELECT sname FROM student");
  113. // while (rs.next()) {
  114. // String sname = rs.getString("sname");
  115. // System.out.println(sname);
  116. // conn.close();
  117. // }
  118. // } catch (Exception e) {
  119. // System.out.println("Kaaaaaaaan inte hitta database driver class: " + e);
  120. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement