package org.webchat.repository; import java.sql.*; public class DatabaseConnection {…
- User
package org.webchat.repository;
import java.sql.*;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/anonymous_chat_db"; private static final String USER = "daniyar"; private static final String PASSWORD = ""; public static Connection getConnection() throws SQLException { try { Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } try(Connection connection = DriverManager.getConnection(URL, USER, PASSWORD); Statement stmt = connection.createStatement(); ) { String sql = "CREATE DATABASE STUDENTS"; stmt.executeUpdate(sql); System.out.println("Database created successfully..."); } catch (SQLException e) { e.printStackTrace(); } return DriverManager.getConnection(URL, USER, PASSWORD); } public static Connection getConnectionPostrges() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException { Class.forName("org.postgresql.Driver"); return DriverManager.getConnection(URL, USER, PASSWORD); } public static boolean createAllTable(){ // MariaBD String createTableChats = "CREATE TABLE chats(id_chat VARCHAR(255) PRIMARY KEY, name VARCHAR(255) NOT NULL)"; String createTableMessages = "CREATE TABLE messages(id_message BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, id_chat VARCHAR(255) NOT NULL REFERENCES chats(id_chat), id_from VARCHAR(255) NOT NULL, content TEXT, date TIMESTAMP)"; String createTableUserChats = "CREATE TABLE user_chats(id_user VARCHAR(255) NOT NULL REFERENCES users(id_user), id_chat VARCHAR(255) NOT NULL REFERENCES chats(id_chat))"; String createTableUsers = "CREATE TABLE users(id_user VARCHAR(255) PRIMARY KEY, username VARCHAR(255) NOT NULL UNIQUE, password_hash VARCHAR(255) NOT NULL)"; try (Connection connection = DatabaseConnection.getConnection(); PreparedStatement chatsStatement = connection.prepareStatement(createTableChats); PreparedStatement messagesStatement = connection.prepareStatement(createTableMessages); PreparedStatement userChatsStatement = connection.prepareStatement(createTableUserChats); PreparedStatement userStatement = connection.prepareStatement(createTableUsers)) { ResultSet userResultSet1 = chatsStatement.executeQuery(); ResultSet userResultSet2 = messagesStatement.executeQuery(); ResultSet userResultSet3 = userChatsStatement.executeQuery(); ResultSet userResultSet4 = userStatement.executeQuery(); return (userResultSet1.next() && userResultSet2.next() && userResultSet3.next() && userResultSet4.next()); } catch (SQLException e) { return false; } }} Как создать бд внутри кода
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.