Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 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=Vouchery_valis";
  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.              JOptionPane.showMessageDialog(null, "Chyba při navazování spojení se serverem", "Chyba", JOptionPane.ERROR_MESSAGE);
  11.         }
  12.     }
  13.  
  14.        
  15.  
  16.    public void dotaz(){
  17.        try {
  18.             String sql = "SELECT * FROM NazevTvojiTabulky";
  19.  
  20.             Statement stmt = connection.createStatement();
  21.             ResultSet result = stmt.executeQuery(sql);
  22.             while (result.next()) {
  23.                // např. když tam bude sloupec Nazev tak: String nazev = result.getString("Nazev");
  24.                // ....
  25.             }
  26.         } catch (SQLException e) {
  27.             System.err.println(e);
  28.         }
  29.     }
  30.  
  31. // v kodu potom jen:  
  32.  
  33. // vytvorConnection();
  34. // dotaz();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement