Advertisement
Guest User

Untitled

a guest
Jun 10th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. package ro.scoalainformala.helper;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9.  
  10. public class DBHelper {
  11.  
  12.     public static Connection getConnection() {
  13.  
  14.         Properties connectionProperties = new Properties();
  15.         connectionProperties.put("user", "root");
  16.         connectionProperties.put("password", "She is going places1");
  17.  
  18.         String connString = "jdbc:mysql://localhost:3306/bank_curs";
  19.         try {
  20.             Connection conn = DriverManager.getConnection(connString, connectionProperties);
  21.             conn.setAutoCommit(false);
  22.             Statement stmt = conn.createStatement();
  23.             ResultSet rs = stmt.executeQuery("select * from bank");
  24.             while (rs.next()) {
  25.                 int id = rs.getInt("id");
  26.                 String name = rs.getString("name");
  27.                 String countryCode = rs.getString("country_code");
  28.                 System.out.println(id + " " + name + " " + countryCode);
  29.  
  30.             }
  31.             conn.close();
  32.         } catch (SQLException e) {
  33.             e.printStackTrace();
  34.             return null;
  35.         }
  36.     }
  37.  
  38.     public static void closeConnection(Connection conn) {
  39.         try {
  40.             conn.close();
  41.         } catch (SQLException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement