Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. package mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. public class InsertMain {
  9. private static final String query1 = "CREATE TABLE user" +
  10. "(" +
  11. "id int primary key auto_increment," +
  12. "name varchar(200)" +
  13. ")";
  14. private static final String query2 = "CREATE TABLE `order` " +
  15. "(" +
  16. "id int primary key auto_increment," +
  17. "description varchar(200)," +
  18. "user_id int," +
  19. "foreign key(user_id) references user(id)" +
  20. ")";
  21. private static final String query3 = "";
  22. private static final String query4 = "";
  23. private static final String query5 = "";
  24.  
  25. public static void main(String[] args){
  26. try(Connection connection = craeteConnection()){
  27. createTable(connection, query1);
  28. createTable(connection, query2);
  29. // createTable(connection, query3);
  30. // createTable(connection, query4);
  31. // createTable(connection, query5);
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. private static void createTable(Connection connection, String query) throws SQLException {
  38. try(PreparedStatement preparedStatement =
  39. connection.prepareStatement(query)){
  40. preparedStatement.executeUpdate();
  41. System.out.println("Insert was successful");
  42. }
  43. }
  44.  
  45. private static Connection craeteConnection() throws SQLException {
  46. return DriverManager.getConnection("jdbc:mysql://localhost:3306/exam_test", "root", "root");
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement