Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. package a_Zadania.a_Dzien_1.a_Tworzenie_baz_i_tabel;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class Main2 {
  9. //Zapisz w poniższej zmiennej kod zapytania SQL tworzącego pierwszą tabelę
  10. public static String queryCreateTableProducts = ""+
  11. "Create table products(" +
  12. "id int auto_increment,"+
  13. "name varchar(255),"+
  14. "price decimal(10,2),"+
  15. "Primary key(id)"+
  16. ")";
  17.  
  18. //Zapisz w poniższej zmiennej kod zapytania SQL tworzącego drugą tabelę
  19. public static String queryCreateTableOrders = "Create table `orders` (id int auto_increment primary key, description text);";
  20.  
  21.  
  22. //Zapisz w poniższej zmiennej kod zapytania SQL tworzącego trzecią tabelę
  23. public static String queryCreateTableClients = "Create table `clients` (`id` int auto_increment primary key, `name` varchar(150), `surname` varchar(150) );";
  24.  
  25.  
  26. // public static String query = "Create database products_ex character set UTF8 collate utf8_unicode_ci;";
  27.  
  28. public static void createTable(String query){
  29. String connUrl = "jdbc:mysql://localhost:3306/products_ex?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
  30. try (Connection conn = DriverManager.getConnection(
  31. connUrl,
  32. "root", "coderslab")) {
  33.  
  34. Statement st = conn.createStatement();
  35. st.executeUpdate(query);
  36.  
  37. } catch (SQLException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. public static void main(String[] args) {
  42. createTable(queryCreateTableClients);
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement