Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 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 bazadanych;
  7.  
  8. import java.sql.*;
  9. import java.util.*;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.SQLException;
  14. import java.util.Properties;
  15.  
  16. public class BazaDanych {
  17. // init database constants
  18. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  19. private static final String DATABASE_URL = "jdbc:mysql://www.serwer1600151.home.pl/";
  20. private static final String USERNAME = "20748517_0000004";
  21. private static final String PASSWORD = "2&QPqua8rQDY";
  22. private static final String MAX_POOL = "250";
  23.  
  24. // init connection object
  25. private Connection connection;
  26. // init properties object
  27. private Properties properties;
  28.  
  29. // create properties
  30. private Properties getProperties() {
  31. if (properties == null) {
  32. properties = new Properties();
  33. properties.setProperty("user", USERNAME);
  34. properties.setProperty("password", PASSWORD);
  35. properties.setProperty("MaxPooledStatements", MAX_POOL);
  36. }
  37. return properties;
  38. }
  39.  
  40. // connect database
  41. public Connection connect() {
  42. if (connection == null) {
  43. try {
  44. Class.forName(DATABASE_DRIVER);
  45. connection = DriverManager.getConnection(DATABASE_URL, getProperties());
  46. } catch (ClassNotFoundException | SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. return connection;
  51. }
  52.  
  53. // disconnect database
  54. public void disconnect() {
  55. if (connection != null) {
  56. try {
  57. connection.close();
  58. connection = null;
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. }
  64.  
  65. public static void main(String [ ] args){
  66. BazaDanych mysqlConnect = new BazaDanych();
  67. String sql = "INSERT INTO `20748517_0000004`.`user` (`Login`, `Password`) VALUES ('Andriu', 'asd')";
  68. try {
  69. PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql);
  70. statement.execute();
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. } finally {
  74. mysqlConnect.disconnect();
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement