Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class Driver {
- private static final String url = "jdbc:mysql://localhost:3306/process_monitoring_database";
- private static final String user = "root";
- private static final String password = "12345";
- static Connection mConnection;
- static Statement mStatement;
- public static void main(String[] args) {
- try {
- //Get a connection to database
- //mConnection = DriverManager.getConnection(url, user, password);
- mConnection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=12345");
- //Create a statement
- mStatement = mConnection.createStatement();
- //Execure SQL query
- /*String sql = "INSERT INTO employees "
- + " (last_name, first_name, email)"
- + " VALUES ('"data"', 'Data2', 'd@ua.fm')";
- statement.executeUpdate(sql);*/
- //createDatabase();
- addAccount("User2", "sssssss");
- //addUser(1, "vvvvvvvvvvv");
- //addUser(1, "aaaaaaaaa");
- //ResultSet resultSet = myStmt.executeQuery("INSERT INTO `demo`.`employees` (`last_name`, `first_name`, `email`) VALUES ('Ttttt', 'Dm', 'dimqa123@ua.fm'");
- /*while(resultSet.next()) {
- System.out.println(resultSet.getString("last_name") + " " + resultSet.getString("first_name") + " " + resultSet.getString("email") + " ");
- }*/
- } catch(Exception exc) {
- exc.printStackTrace();
- }
- }
- public static void createDatabase() {
- try {
- int result = mStatement.executeUpdate("CREATE DATABASE IF NOT EXISTS process_monitoring_database DEFAULT CHARACTER SET utf8");
- System.out.println("Create database " + result);
- /*String sql = "CREATE TABLE IF NOT EXISTS process_monitoring_database.account ( " +
- " account_id INT NOT NULL AUTO_INCREMENT, " +
- " login VARCHAR(50) NOT NULL, " +
- " password VARCHAR(50) NOT NULL, " +
- " PRIMARY KEY ( account_id )) " +
- " UNIQUE KEY login_UNIQUE (login ASC)) " +
- " ENGINE = InnoDB " +
- " DEFAULT CHARACTER SET = utf8;";*/
- String sql = "CREATE TABLE process_monitoring_database.account ( " +
- " account_id INT NOT NULL AUTO_INCREMENT, " +
- " login VARCHAR(50) NOT NULL, " +
- " password VARCHAR(50) NOT NULL, " +
- " PRIMARY KEY (account_id), " +
- " UNIQUE INDEX account_id_UNIQUE (account_id ASC), " +
- " UNIQUE INDEX login_UNIQUE (login ASC)) " +
- " ENGINE = InnoDB " +
- " DEFAULT CHARACTER SET = utf8;";
- mStatement.executeUpdate(sql);
- sql = "CREATE TABLE IF NOT EXISTS process_monitoring_database.user ( " +
- " user_id INT NOT NULL AUTO_INCREMENT, " +
- " account_id INT NOT NULL, " +
- " user_name VARCHAR(50) NOT NULL, " +
- " PRIMARY KEY ( user_id ), " +
- " INDEX account_id_idx (account_id ASC), " +
- " CONSTRAINT account_id " +
- " FOREIGN KEY (account_id) " +
- " REFERENCES process_monitoring_database.account ( account_id ) " +
- " ON DELETE CASCADE " +
- " ON UPDATE CASCADE) " +
- " ENGINE = InnoDB " +
- " DEFAULT CHARACTER SET = utf8;";
- mStatement.executeUpdate(sql);
- sql = "CREATE TABLE IF NOT EXISTS process_monitoring_database.device ( " +
- " device_id INT NOT NULL AUTO_INCREMENT, " +
- " user_id INT NOT NULL, " +
- " token VARCHAR(255) NOT NULL, " +
- " apps LONGTEXT NULL, " +
- " latitude FLOAT NULL, " +
- " longtitude FLOAT NULL, " +
- " PRIMARY KEY (device_id), " +
- " INDEX user_id_idx (user_id ASC), " +
- " CONSTRAINT user_id " +
- " FOREIGN KEY ( user_id ) " +
- " REFERENCES process_monitoring_database.user ( user_id ) " +
- " ON DELETE NO ACTION " +
- " ON UPDATE NO ACTION) " +
- " ENGINE = InnoDB " +
- " DEFAULT CHARACTER SET = utf8;";
- mStatement.executeUpdate(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void addAccount(String login, String password) {
- String sql = "INSERT INTO process_monitoring_database.account " +
- "(`login`, `password`) VALUES (?, ?)";
- try {
- PreparedStatement preparedStatement = mConnection.prepareStatement(sql);
- preparedStatement.setString(1, login);
- preparedStatement.setString(2, password);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void addUser(int accountID, String userName) {
- String sql = "INSERT INTO process_monitoring_database.user " +
- " (account_id, user_name) VALUES (?, ?)";
- try {
- PreparedStatement preparedStatement = mConnection.prepareStatement(sql);
- preparedStatement.setInt(1, accountID);
- preparedStatement.setString(2, userName);
- preparedStatement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement