Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.ResultSet;
- /* Dane piwerwszych dwoch studentow
- ID: 1, name: Zenon, surname: Martyniuk, student_group: 2
- ID: 2, name: Bartłomiej, surname: Misiewicz, student_group: 4
- */
- public class Main{
- static final String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_database";
- static final String USER = "cackoa_user";
- static final String PASS = "cackoa_password";
- public static void main(String args[]){
- Connection polaczenie = null;
- java.sql.Statement stmt = null;
- try{
- // Połączenie z bazą danych
- System.out.println("Connecting to database...");
- polaczenie = DriverManager.getConnection(DB_URL,USER,PASS);
- // Stworzenie zapytania oraz jego wykonanie
- System.out.println("Creating statement...");
- stmt = polaczenie.createStatement();
- String sql;
- sql = "SELECT * FROM student";
- ResultSet rs = stmt.executeQuery(sql);
- // Ekstrakcja wyników
- while(rs.next()){
- //Retrieve by column name
- int id = rs.getInt("id");
- String name = rs.getString("name");
- String surname = rs.getString("surname");
- int student_group = rs.getInt("student_group");
- //Wyświetlenie danych:
- System.out.print("ID: " + id);
- System.out.print(", name: " + name);
- System.out.print(", surname: " + surname);
- System.out.println(", student_group: " + student_group);
- }
- // Czyszczenie po sobie
- rs.close();
- stmt.close();
- polaczenie.close();
- }catch(SQLException se){
- //Errory JDBC
- se.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- //finally block - by pozamykać resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(polaczenie!=null)
- polaczenie.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Koniec!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement