Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQLAccess {
  10.  
  11.     private Connection connect = null;
  12.     private Statement statement = null;
  13.     private PreparedStatement preparedStatement = null;
  14.     private ResultSet resultSet = null;
  15.  
  16.     public String getResultSet() throws SQLException {
  17.         resultSet = statement.executeQuery("select * from examen.material");
  18.         return writeResultSet(resultSet);
  19.     }
  20.  
  21.     public void inserare(String model, String culoare, String lungime) throws Exception {
  22.         preparedStatement = connect
  23.                 .prepareStatement("insert into examen.material values (default, ?, ?, ?)");
  24.         preparedStatement.setString(1, model);
  25.         preparedStatement.setString(2, culoare);
  26.         preparedStatement.setString(3, lungime);
  27.         preparedStatement.executeUpdate();
  28.     }
  29.  
  30.     public void readDataBase() throws Exception {
  31.         try {
  32.             // Incarca driverul MYSQL
  33.             Class.forName("com.mysql.jdbc.Driver");
  34.  
  35.             // Creeaza conexiunea cu baza de data
  36.             connect = DriverManager.getConnection("jdbc:mysql://localhost/examen?" + "user=root&password=");
  37.  
  38.             // Statementurile ne ajuta sa facem query
  39.             statement = connect.createStatement();
  40.  
  41.             // Afisam toata baza de date
  42.             resultSet = statement.executeQuery("select * from examen.material");
  43.             writeResultSet(resultSet);
  44.  
  45.             System.out.println("---------------------------------------");
  46.  
  47.         } catch (Exception e) {
  48.             throw e;
  49.         } finally {
  50.             close();
  51.         }
  52.  
  53.     }
  54.  
  55.     private String writeResultSet(ResultSet resultSet) throws SQLException {
  56.         // ResultSet is initially before the first data set
  57.         String str = "NU AFISEAZA NIMIC";
  58.         while (resultSet.next()) {
  59.             // It is possible to get the columns via name
  60.             // also possible to get the columns via the column number
  61.             // which starts at 1
  62.             // e.g. resultSet.getSTring(2);
  63.  
  64.             String model = resultSet.getString("model");
  65.             String culoare = resultSet.getString("culoare");
  66.             String lungime = resultSet.getString("lungime");
  67.             str = str + ("\nModel: " + model + "Culoare: " + culoare + "Lungime: " + lungime);
  68.         }
  69.  
  70.         return str;
  71.     }
  72.  
  73.     // You need to close the resultSet
  74.     private void close() {
  75.         try {
  76.             if (resultSet != null) {
  77.                 resultSet.close();
  78.             }
  79.  
  80.             if (statement != null) {
  81.                 statement.close();
  82.             }
  83.  
  84.             if (connect != null) {
  85.                 connect.close();
  86.             }
  87.         } catch (Exception e) {
  88.  
  89.         }
  90.     }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement