Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Connection getMySqlConnection() {
- String urlMySq = "jdbc:mysql://localhost:3306/mysqldb";
- String login = "mysql";
- String password = "mysql_password";
- return getMySqlConnection ("com.mysql.jdbc.Driver", "jdbc:mysql://hostname:port/dbname","username", "password");
- }
- private static Connection getMySqlConnection(String dbmsqlDriver,
- String dbmysqlConnection,
- String dbmysqlUser,
- String dbmysqlPassword) {
- Connection connection = null;
- try {
- Class.forName (dbmsqlDriver);
- } catch (ClassNotFoundException e) {
- System.out.println (e.getMessage ());
- }
- try {
- connection = DriverManager.getConnection (dbmysqlConnection, dbmysqlUser, dbmysqlPassword);
- return connection;
- } catch (SQLException e) {
- System.out.println (e.getMessage ());
- }
- return connection;
- }
- public static void createMySqlAddressTable(Connection dbmysqlConnection) {
- Statement statement = null;
- String createTableSQL = "CREATE TABLE address("
- + "address_id INTEGER NOT NULL, "
- + "country VARCHAR(30) NOT NULL, "
- + "city VARCHAR(20) NOT NULL, "
- + "street VARCHAR(250) NOT NULL, "
- + "house_number INTEGER NOT NULL, "
- + "house_suffix VARCHAR(20) NOT NULL, "
- + "poste_code INTEGER NOT NULL, "
- + "PRIMARY KEY (address_id) "
- + ")";
- try {
- statement = dbmysqlConnection.createStatement ();
- // выполнить SQL запрос
- statement.execute (createTableSQL);
- System.out.println ("Table \"dbuser\" is created!");
- } catch (SQLException e) {
- System.out.println (e.getMessage ());
- } finally {
- if (statement != null) {
- try {
- statement.close ();
- } catch (SQLException e) {
- e.printStackTrace ();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement