Advertisement
Guest User

pslab2

a guest
Mar 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package l2ps;
  7.  
  8. import java.sql.*;
  9.  
  10. /**
  11. *
  12. * @author Student
  13. */
  14. public class L2ps {
  15.  
  16. /**
  17. * @param args the command line arguments
  18. */
  19. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  20. static final String DB_URL = "jdbc:mysql://localhost:3306/l2ps";
  21.  
  22. // Database credentials
  23. static final String USER = "root";
  24. static final String PASS = "root";
  25.  
  26. public static void main(String[] args) {
  27. Connection conn = null;
  28. Statement stmt = null;
  29. try{
  30. //STEP 2: Register JDBC driver
  31. Class.forName("com.mysql.jdbc.Driver");
  32.  
  33. //STEP 3: Open a connection
  34. System.out.println("Connecting to database...");
  35. conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
  36.  
  37. //STEP 4: Execute a query
  38. System.out.println("Creating statement...");
  39. stmt = (Statement) conn.createStatement();
  40. String sql;
  41. sql = "SELECT idstudent, nume, data, adresa FROM student";
  42. ResultSet rs = stmt.executeQuery(sql);
  43.  
  44. //STEP 5: Extract data from result set
  45. while(rs.next()){
  46. //Retrieve by column name
  47. int id = rs.getInt("idstudent");
  48. String nume = rs.getString("nume");
  49. int data = rs.getInt("data");
  50. String adresa = rs.getString("adresa");
  51.  
  52. //Display values
  53. System.out.print("ID: " + id);
  54. System.out.print(", nume: " + nume);
  55. System.out.print(", data: " + data);
  56. System.out.println(", adresa: " + adresa);
  57.  
  58. Student student = new Student(7, "Claudia", 1993, "Camin");
  59. insert(conn, student);
  60. }
  61. //STEP 6: Clean-up environment
  62. rs.close();
  63. stmt.close();
  64. conn.close();
  65. }catch(SQLException se){
  66. //Handle errors for JDBC
  67. se.printStackTrace();
  68. }catch(Exception e){
  69. //Handle errors for Class.forName
  70. e.printStackTrace();
  71. }finally{
  72. //finally block used to close resources
  73. try{
  74. if(stmt!=null)
  75. stmt.close();
  76. }catch(SQLException se2){
  77. }// nothing we can do
  78. try{
  79. if(conn!=null)
  80. conn.close();
  81. }catch(SQLException se){
  82. se.printStackTrace();
  83. }//end finally try
  84. }//end try
  85. System.out.println("Goodbye!");
  86.  
  87. }
  88.  
  89. public static void insert(Connection conn, Student student) throws SQLException{
  90. String statement = "Insert into student (idstudent, nume, data, adresa) values (?,?,?,?)";
  91. PreparedStatement prepSt = conn.prepareStatement(statement);
  92. prepSt.setInt(1, student.getId());
  93. prepSt.setString(2, student.getNume());
  94. prepSt.setInt(3, student.getData());
  95. prepSt.setString(4, student.getAdresa());
  96. prepSt.executeUpdate();
  97. }
  98.  
  99. public static void delete(Connection conn, Student student) throws SQLException{
  100. String statement = "Delete from student where Id=?";
  101. PreparedStatement prepSt = conn.prepareStatement(statement);
  102. prepSt.setInt(1, student.getId());
  103. }
  104.  
  105. public static void update(Connection conn, Student student) throws SQLException{
  106. String statement = "Update student set nume=?, data=?, adresa=? where id=?";
  107. PreparedStatement prepSt = conn.prepareStatement(statement);
  108. prepSt.setString(1, student.getNume());
  109. prepSt.setInt(2, student.getData());
  110. prepSt.setString(3, student.getAdresa());
  111. prepSt.executeUpdate();
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement