Guest User

Untitled

a guest
Sep 26th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package forelesning_1;
  2.  
  3.  
  4. // import db.ConnectToDB;
  5.  
  6. import java.sql.*;
  7.  
  8. public class FirstDBConnection {
  9.  
  10.  
  11. public static void main(String[] args) throws SQLException {
  12. String brukernavn = args[0];
  13. String passord = args[1];
  14. String url = "jdbc:mysql://mysql.nith.no/marste";
  15. Connection con = null;
  16.  
  17. try {
  18. Class.forName("com.mysql.jdbc.Driver");
  19. //System.out.println("A");
  20. } catch (ClassNotFoundException e) {
  21.  
  22. e.printStackTrace();
  23. }
  24.  
  25. try {
  26. con = DriverManager.getConnection(url, brukernavn, passord);
  27. System.out.println("Fikk kontakt");
  28. System.out.println(" ");
  29. System.out.println(" ");
  30. } catch (SQLException e) {
  31.  
  32. e.printStackTrace();
  33. }
  34.  
  35.  
  36. Statement stmt = con.createStatement();
  37. ResultSet res = stmt.executeQuery("select * from bokliste");
  38.  
  39. // Kun printe ut bøkene
  40. /*while (res.next()) {
  41. String fornavn = res.getString(2);
  42. System.out.println(fornavn);
  43. } */
  44.  
  45. // Printe ut hele lista.
  46. while (res.next()) {
  47. for (int i = 1; i <= 5; i++) {
  48. System.out.print(res.getString(i) + "\t");
  49. }
  50. System.out.println();
  51. }
  52.  
  53. System.out.println();
  54. System.out.println();
  55.  
  56.  
  57. ResultSet res2 = stmt.executeQuery("select * from varer");
  58.  
  59. while (res2.next()) {
  60. for (int i = 1; i <= 4; i++) {
  61. System.out.print(res2.getString(i) + "\t");
  62. }
  63. System.out.println();
  64.  
  65.  
  66. }
  67. } // End of main
  68. } // end of class
Add Comment
Please, Sign In to add comment