Advertisement
Guest User

Untitled

a guest
Feb 9th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. //Santeri Tuomisto
  2. //Teht 3b
  3.  
  4. import java.sql.*;
  5. import java.util.Scanner;
  6.  
  7. public class Teht3b {
  8.  
  9. private static final String PROTOKOLLA = "jdbc:postgresql:";
  10. private static final String PALVELIN = "dbstud2.sis.uta.fi";
  11. private static final int PORTTI = 5432;
  12. private static final String TIETOKANTA = "st422465";
  13. private static final String KAYTTAJA = "st422465";
  14. private static final String SALASANA = "1234";
  15.  
  16. public static void main(String args[]) {
  17.  
  18. Connection con = null;
  19. try {
  20. con = DriverManager.getConnection(PROTOKOLLA + "//" + PALVELIN + ":" + PORTTI + "/" + TIETOKANTA, KAYTTAJA, SALASANA);
  21.  
  22. PreparedStatement prstmt = con.prepareStatement("SELECT opiskelija.opiskelijanumero, opiskelija.nimi, opiskelija.paaaine " +
  23. "FROM yk.opiskelija, yk.suoritukset " +
  24. "WHERE opiskelija.opiskelijanumero = suoritukset.opiskelijanumero AND kurssinumero = ? ");
  25.  
  26. prstmt.clearParameters();
  27. System.out.println("Syötä kurssinumero: ");
  28. Scanner sc = new Scanner(System.in);
  29. int value = sc.nextInt();
  30.  
  31. prstmt.setInt(1, value);
  32.  
  33. ResultSet rs = prstmt.executeQuery();
  34.  
  35. ResultSetMetaData rsmd = rs.getMetaData();
  36.  
  37. int numberOfColumns = rsmd.getColumnCount();
  38. System.out.println("onro, nimi, pääaine");
  39. while (rs.next()) {
  40. for (int i = 1; i <= numberOfColumns; i++) {
  41. String s = rs.getString(i);
  42. if(rs.wasNull())
  43. System.out.println("NULL");
  44. else
  45. System.out.print(s + " ");
  46. }
  47. System.out.println("");
  48. }
  49.  
  50.  
  51.  
  52. prstmt.close();
  53. }
  54. catch (SQLException poikkeus) {
  55. System.out.println("Tapahtui seuraava virhe: " + poikkeus.getMessage());
  56. }
  57.  
  58. if (con != null) try { // jos yhteyden luominen ei onnistunut, con == null
  59. con.close();
  60. } catch(SQLException poikkeus) {
  61. System.out.println("Yhteyden sulkeminen tietokantaan ei onnistunut. Lopetetaan ohjelman suoritus.");
  62. return;
  63. }
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement