SHARE
TWEET

CATA

a guest May 27th, 2019 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 ander;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14.  
  15. /**
  16.  *
  17.  * @author student
  18.  */
  19. public class johan {
  20.     Connection conn;
  21.    // JDBC driver name and database URL
  22.    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
  23.    static final String DB_URL = "jdbc:mysql://dbstud.cunbm.utcluj.ro:23306/sicall-andercaucornel";
  24.  
  25.    //  Database credentials
  26.    static final String USER = "sicall-andercaucornel";
  27.    static final String PASS = "Garcea";
  28.    
  29.    ArrayList<Student> studenti;
  30.  
  31.     public johan() {
  32.         this.p();
  33.        
  34.     }
  35.    
  36.    public void p() {  
  37.    Statement stmt = null;
  38.    try{
  39.       //STEP 2: Register JDBC driver
  40.       Class.forName("com.mysql.jdbc.Driver");
  41.  
  42.       //STEP 3: Open a connection
  43.       System.out.println("Connecting to database...");
  44.       conn = DriverManager.getConnection(DB_URL,USER,PASS);
  45.  
  46.       //STEP 4: Execute a query
  47.       System.out.println("Creating statement...");
  48.       stmt = conn.createStatement();
  49.       String sql;
  50.       sql = "SELECT id, nume, CNP,sex,medie FROM studenti";
  51.       ResultSet rs = stmt.executeQuery(sql);
  52.  
  53.       studenti = new ArrayList<Student>();
  54.       //STEP 5: Extract data from result set
  55.       while(rs.next()){
  56.          //Retrieve by column name
  57.          int id  = rs.getInt("id");
  58.          String nume = rs.getString("nume");
  59.          String CNP = rs.getString("CNP");
  60.          Double medie = rs.getDouble("medie");
  61.  
  62.          Student s = new Student(id, nume, CNP, medie);
  63.          studenti.add(s);
  64.          //Display values
  65.          System.out.print("ID: " + id);
  66.          System.out.print(", Nume : " + nume);
  67.          System.out.print(", CNP : " + CNP);
  68.          System.out.println(", Medie : " + medie);
  69.       }
  70.       //STEP 6: Clean-up environment
  71.       rs.close();
  72.       stmt.close();
  73.       conn.close();
  74.    }catch(SQLException se){
  75.       //Handle errors for JDBC
  76.       se.printStackTrace();
  77.    }catch(Exception e){
  78.       //Handle errors for Class.forName
  79.       e.printStackTrace();
  80.    }finally{
  81.       //finally block used to close resources
  82.       try{
  83.          if(stmt!=null)
  84.             stmt.close();
  85.       }catch(SQLException se2){
  86.       }// nothing we can do
  87.       try{
  88.          if(conn!=null)
  89.             conn.close();
  90.       }catch(SQLException se){
  91.          se.printStackTrace();
  92.       }//end finally try
  93.    }//end try
  94.    System.out.println("Goodbye!");
  95. //end main
  96.    
  97. }
  98.    public ArrayList<Student> getStudenti() {
  99.        return this.studenti;
  100.    }
  101.    
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top