Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1.     public void vytvorConnection() {
  2.         String username ="student";
  3.         String password ="Baterie15";
  4.         String url ="jdbc:sqlserver://dbs.pf.jcu.cz;databaseName=TVUJ_NAZEV_DB";
  5.         try {  
  6.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  7.             connection = DriverManager.getConnection(url, username,password);
  8.         } catch (SQLException | ClassNotFoundException e) {
  9.              System.err.println(e);
  10.         }
  11.     }
  12.  
  13.        
  14.  
  15.    public void dotaz(){
  16.        try {
  17.             String sql = "SELECT * FROM NazevTvojiTabulky";
  18.  
  19.             Statement stmt = connection.createStatement();
  20.             ResultSet result = stmt.executeQuery(sql);
  21.             while (result.next()) {
  22.                // např. když tam bude sloupec Nazev tak: String nazev = result.getString("Nazev");
  23.                // ....
  24.             }
  25.         } catch (SQLException e) {
  26.             System.err.println(e);
  27.         }
  28.     }
  29.  
  30. // v kodu potom jen:  
  31.  
  32. // vytvorConnection();
  33. // dotaz();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement