Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Santeri Tuomisto
- //Teht 3b
- import java.sql.*;
- import java.util.Scanner;
- public class Teht3b {
- private static final String PROTOKOLLA = "jdbc:postgresql:";
- private static final String PALVELIN = "dbstud2.sis.uta.fi";
- private static final int PORTTI = 5432;
- private static final String TIETOKANTA = "st422465";
- private static final String KAYTTAJA = "st422465";
- private static final String SALASANA = "1234";
- public static void main(String args[]) {
- Connection con = null;
- try {
- con = DriverManager.getConnection(PROTOKOLLA + "//" + PALVELIN + ":" + PORTTI + "/" + TIETOKANTA, KAYTTAJA, SALASANA);
- PreparedStatement prstmt = con.prepareStatement("SELECT opiskelija.opiskelijanumero, opiskelija.nimi, opiskelija.paaaine " +
- "FROM yk.opiskelija, yk.suoritukset " +
- "WHERE opiskelija.opiskelijanumero = suoritukset.opiskelijanumero AND kurssinumero = ? ");
- prstmt.clearParameters();
- System.out.println("Syötä kurssinumero: ");
- Scanner sc = new Scanner(System.in);
- int value = sc.nextInt();
- prstmt.setInt(1, value);
- ResultSet rs = prstmt.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int numberOfColumns = rsmd.getColumnCount();
- System.out.println("onro, nimi, pääaine");
- while (rs.next()) {
- for (int i = 1; i <= numberOfColumns; i++) {
- String s = rs.getString(i);
- if(rs.wasNull())
- System.out.println("NULL");
- else
- System.out.print(s + " ");
- }
- System.out.println("");
- }
- prstmt.close();
- }
- catch (SQLException poikkeus) {
- System.out.println("Tapahtui seuraava virhe: " + poikkeus.getMessage());
- }
- if (con != null) try { // jos yhteyden luominen ei onnistunut, con == null
- con.close();
- } catch(SQLException poikkeus) {
- System.out.println("Yhteyden sulkeminen tietokantaan ei onnistunut. Lopetetaan ohjelman suoritus.");
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement