Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package koti3;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import com.mysql.jdbc.PreparedStatement;
  10.  
  11. public class DBConnect {
  12.  
  13.  
  14.  
  15. public static void main(String[] args) {
  16.  
  17. // TODO Auto-generated method stub
  18. System.out.println("Valitse 1) Lisää 2) Poista 3) Muuta 4) Päivitä Lista");
  19. Scanner scanner = new Scanner(System.in);
  20. int command = scanner.nextInt();
  21. System.out.println(command);
  22. //Here switch/case depending what command
  23. //
  24. }
  25.  
  26.  
  27. String kysely = "SELECT asiakasID, asiakas, osoite, postinumero,toimipaikka, sahkoposti FROM asiakas1";
  28.  
  29. try {
  30. // ladataan ajuri
  31. Class.forName("com.mysql.jdbc.Driver");
  32. } catch (Exception e) {
  33. System.out.println("Virhe tietokanta-ajuria ladattaessa " + e);
  34. System.exit(1); // lopetetaan ohjelma tähän
  35. }
  36. //Tietokantayhteys Connection
  37. try (Connection con = DriverManager.getConnection(
  38. "jdbc:mysql://mysql.cc.puv.fi:3306/e1400519_java", "e1400519",
  39. "d9j3GZJpcPWv")) {
  40.  
  41. //Statement SQL-lauseita ja ResultSet tulosjoukkoa varten
  42. try (Statement stm = con.createStatement();
  43. ResultSet rs = stm.executeQuery(kysely)) {
  44.  
  45. System.out.println("ASIAKASTIEDOT\n"); // tulostetaan näyttölle
  46. //Käydään tulosjoukko ResultSet läpi
  47. while (rs.next()) {
  48. String asiakasID = rs.getString(1); // tai rs.getString("asiakasID");
  49. String asiakas = rs.getString(2);
  50. String osoite = rs.getString(3);
  51. String postinumero = rs.getString(4);
  52. String toimipaikka = rs.getString(5);
  53. String sahkoposti = rs.getString(6);
  54.  
  55. System.out.println(asiakasID + ": " + asiakas + ", "
  56. + osoite + ", " + postinumero
  57. + ", " + toimipaikka
  58. + ", " + sahkoposti + " ");
  59. }
  60. } //try with Statement ja ResultSet päättyy
  61. } //try with Connection päättyy
  62. catch (SQLException e) {
  63. System.out.println("Ongelmia tietokannan kasittelyssa "
  64. + e.getMessage());
  65.  
  66. }
  67. }
  68. }
  69.  
  70. public void insert(Connection con, String asiakasID, String asiakas, String osoite, String postinumero, String toimipaikka, String sahkoposti) throws SQLException {
  71. //Tietokantayhteys Connection
  72. try (PreparedStatement ps = (PreparedStatement) con.prepareStatement(
  73. "INSERT INTO asiakas(asiakasID, asiakas, osoite, postinumero, toimipaikka, sahkoposti) VALUES(?, ?, ?, ?, ?, ?, ?)")){
  74. //ps = con.prepareStatement("Lisää uudet asiakastiedot, asiakasID, asiakas, osoite, postinumero, toimipaikka, sahkoposti,");
  75. ps.setString(1, asiakasID);
  76. ps.setString(2, asiakas);
  77. ps.setString(3, osoite);
  78. ps.setString(4, postinumero);
  79. ps.setString(5, toimipaikka);
  80. ps.setString(6, sahkoposti);
  81. int i = ps.executeUpdate();
  82. if (i != 0) {
  83. System.out.println("Inserted");
  84. } else {
  85. System.out.println("not Inserted");
  86. }
  87.  
  88.  
  89.  
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92.  
  93. }
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement