Advertisement
Guest User

Untitled

a guest
Feb 8th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package testi;
  7.  
  8. /**
  9. *
  10. * @author aleks
  11. */
  12. import java.sql.*;
  13.  
  14. public class Testi {
  15.  
  16. // tietokannan ja käyttäjän tiedot
  17.  
  18. // private static final String AJURI = "org.postgresql.Driver";
  19. private static final String PROTOKOLLA = "jdbc:postgresql:";
  20. private static final String PALVELIN = "dbstud2.sis.uta.fi";
  21. private static final int PORTTI = 5432;
  22. private static final String TIETOKANTA = "al424411"; // tähän oma käyttäjätunnus
  23. private static final String KAYTTAJA = "al424411"; // tähän oma käyttäjätunnus
  24. private static final String SALASANA = "666"; // tähän tietokannan salasana
  25.  
  26. public static void main(String args[]) {
  27.  
  28. Connection con = null;
  29.  
  30. try {
  31. con = DriverManager.getConnection(PROTOKOLLA + "//" + PALVELIN + ":" + PORTTI + "/" + TIETOKANTA, KAYTTAJA, SALASANA);
  32.  
  33. PreparedStatement updateArvosana = con.prepareStatement("UPDATE YK.suoritukset SET arvosana 3" +
  34. "WHERE arvosana = 5");
  35. ResultSet rset = updateArvosana.executeQuery();
  36.  
  37.  
  38. updateArvosana.close(); // sulkee automaattisesti myös tulosjoukon rset
  39.  
  40. } catch (SQLException poikkeus) {
  41.  
  42. // Vaihe 3.2: tähän toiminta mahdollisessa virhetilanteessa
  43.  
  44. System.out.println("Tapahtui seuraava virhe: " + poikkeus.getMessage());
  45. }
  46.  
  47. // Vaihe 4: yhteyden sulkeminen
  48.  
  49. if (con != null) try { // jos yhteyden luominen ei onnistunut, con == null
  50. con.close();
  51. } catch(SQLException poikkeus) {
  52. System.out.println("Yhteyden sulkeminen tietokantaan ei onnistunut. Lopetetaan ohjelman suoritus.");
  53. return;
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement