Advertisement
Guest User

Untitled

a guest
Oct 31st, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MySQLAccess {
  4. private Connection connect = null;
  5. private Statement statement = null;
  6. private PreparedStatement preparedStatement = null;
  7. private ResultSet resultSet = null;
  8.  
  9.  
  10. public void writeDataBase(Long ico) throws Exception {
  11. try {
  12. // toto načíta MySQL ovládač, každá DB má vlastný ovládač
  13. Class.forName("com.mysql.jdbc.Driver");
  14.  
  15. // Nastavenie spojenia s DB
  16. connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=8671");
  17. statement = connect.createStatement();
  18.  
  19. // pre vyobrazenie
  20. // resultSet = statement.executeQuery("select * from tssu.faktura");
  21. // writeResultSet(resultSet);
  22.  
  23. preparedStatement = connect.prepareStatement("insert into rpvs.seller values (?)");
  24.  
  25. // ALTER TABLE tablename AUTO_INCREMENT = 1
  26.  
  27. // Parameters start with 1
  28. preparedStatement.setLong(1, ico);
  29. preparedStatement.executeUpdate();
  30.  
  31. // preparedStatement = connect.prepareStatement("SELECT dodavatel, cislo, ico, predmet, cena from tssu.faktura");
  32. // resultSet = preparedStatement.executeQuery();
  33.  
  34. } catch (Exception e) {
  35. throw e;
  36. } finally {
  37. close();
  38. }
  39.  
  40. }
  41.  
  42.  
  43. //vypisanie prvkov databazy
  44. /* private void writeResultSet(ResultSet resultSet) throws SQLException {
  45. // ResultSet is initially before the first data set
  46. while (resultSet.next()) {
  47. // It is possible to get the columns via name
  48. // also possible to get the columns via the column number
  49. // which starts at 1
  50. // e.g. resultSet.getSTring(2);
  51. String user = resultSet.getString("dodavatel");
  52. Integer website = resultSet.getInt("cislo");
  53. System.out.println("User: " + user);
  54. System.out.println("Website: " + website);
  55.  
  56. }
  57. }*/
  58.  
  59. // You need to close the resultSet
  60. private void close() {
  61. try {
  62. if (resultSet != null) {
  63. resultSet.close();
  64. }
  65.  
  66. if (statement != null) {
  67. statement.close();
  68. }
  69.  
  70. if (connect != null) {
  71. connect.close();
  72. }
  73. } catch (Exception e) {
  74.  
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement