Advertisement
PabotMax

MyCode

May 27th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. public static Connection getMySqlConnection() {
  2. String urlMySq = "jdbc:mysql://localhost:3306/mysqldb";
  3. String login = "mysql";
  4. String password = "mysql_password";
  5.  
  6. return getMySqlConnection ("com.mysql.jdbc.Driver", "jdbc:mysql://hostname:port/dbname","username", "password");
  7. }
  8. private static Connection getMySqlConnection(String dbmsqlDriver,
  9. String dbmysqlConnection,
  10. String dbmysqlUser,
  11. String dbmysqlPassword) {
  12. Connection connection = null;
  13. try {
  14. Class.forName (dbmsqlDriver);
  15. } catch (ClassNotFoundException e) {
  16. System.out.println (e.getMessage ());
  17. }
  18. try {
  19. connection = DriverManager.getConnection (dbmysqlConnection, dbmysqlUser, dbmysqlPassword);
  20. return connection;
  21. } catch (SQLException e) {
  22. System.out.println (e.getMessage ());
  23. }
  24. return connection;
  25. }
  26. public static void createMySqlAddressTable(Connection dbmysqlConnection) {
  27. Statement statement = null;
  28.  
  29. String createTableSQL = "CREATE TABLE address("
  30. + "address_id INTEGER NOT NULL, "
  31. + "country VARCHAR(30) NOT NULL, "
  32. + "city VARCHAR(20) NOT NULL, "
  33. + "street VARCHAR(250) NOT NULL, "
  34. + "house_number INTEGER NOT NULL, "
  35. + "house_suffix VARCHAR(20) NOT NULL, "
  36. + "poste_code INTEGER NOT NULL, "
  37. + "PRIMARY KEY (address_id) "
  38. + ")";
  39.  
  40. try {
  41. statement = dbmysqlConnection.createStatement ();
  42.  
  43. // выполнить SQL запрос
  44. statement.execute (createTableSQL);
  45. System.out.println ("Table \"dbuser\" is created!");
  46. } catch (SQLException e) {
  47. System.out.println (e.getMessage ());
  48. } finally {
  49. if (statement != null) {
  50. try {
  51. statement.close ();
  52. } catch (SQLException e) {
  53. e.printStackTrace ();
  54. }
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement