Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class InsertMain {
- private static final String query1 = "CREATE TABLE user" +
- "(" +
- "id int primary key auto_increment," +
- "name varchar(200)" +
- ")";
- private static final String query2 = "CREATE TABLE `order` " +
- "(" +
- "id int primary key auto_increment," +
- "description varchar(200)," +
- "user_id int," +
- "foreign key(user_id) references user(id)" +
- ")";
- private static final String query3 = "";
- private static final String query4 = "";
- private static final String query5 = "";
- public static void main(String[] args){
- try(Connection connection = craeteConnection()){
- createTable(connection, query1);
- createTable(connection, query2);
- // createTable(connection, query3);
- // createTable(connection, query4);
- // createTable(connection, query5);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static void createTable(Connection connection, String query) throws SQLException {
- try(PreparedStatement preparedStatement =
- connection.prepareStatement(query)){
- preparedStatement.executeUpdate();
- System.out.println("Insert was successful");
- }
- }
- private static Connection craeteConnection() throws SQLException {
- return DriverManager.getConnection("jdbc:mysql://localhost:3306/exam_test", "root", "root");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement