Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MysqlCon_with_results {
  4.  
  5. public static void main(String args[]) {
  6.  
  7. try {
  8.  
  9. // Luodaan tietokantayhteys
  10. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kirjat", "root", "");
  11.  
  12. // Luodaan Statement-olio, joka keskustelee tietokannan kanssa
  13. Statement stmt = con.createStatement();
  14.  
  15. // Luodaan tulosjoukko, johon sijoitetaan kyselyn tulos
  16. ResultSet rs = stmt.executeQuery("SELECT * FROM kirja");
  17.  
  18. // Tulosjoukko käydään silmukassa läpi
  19. while (rs.next())
  20. System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getInt(3));
  21.  
  22. con.close();
  23.  
  24. // Varaudutaan virheisiin
  25. } catch (Exception e) {
  26. System.out.println(e);
  27. }
  28.  
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement