Guest User

Untitled

a guest
Oct 14th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import java.sql.*;
  2. public class DBTest {
  3.  
  4. private static final String USERNAME = "root";
  5. private static final String PASSWORD = "root";
  6. private static final String DATABASEURL = "jdbc:mysql://localhost:3306/test";
  7.  
  8. public static void main(String[] args) {
  9. try {
  10. //создаем соеднинение для базы данных
  11. Class.forName("com.mysql.jdbc.Driver");
  12. Connection connection = DriverManager.getConnection(DATABASEURL, USERNAME, PASSWORD);
  13. Statement statement = connection.createStatement();
  14.  
  15. String thisName = "ASFDF", thisGenre="ASDASDAS";
  16. double thisPrice = 23;
  17. statement.executeUpdate("INSERT INTO GameShop(name,genre,price) VALUE " +
  18. "('"+thisName+"','"+thisGenre+"','"+thisPrice+"')");
  19.  
  20. } catch (ClassNotFoundException e) {
  21. e.printStackTrace();
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27.  
  28. public static synchronized void addGames(StandartGame ... gamez) {
  29. try {
  30. //создаем соеднинение для базы данных
  31. Class.forName("com.mysql.jdbc.Driver");
  32. Connection connection = DriverManager.getConnection(DATABASEURL, USERNAME, PASSWORD);
  33. Statement statement = connection.createStatement();
  34.  
  35. String thisName, thisGenre;
  36. double thisPrice;
  37.  
  38. for (StandartGame stg : gamez) {
  39. thisName = stg.getName();
  40. thisGenre = stg.getGenre();
  41. thisPrice = stg.getPrice();
  42. statement.executeUpdate("insert into GameShop (name, genre, price) VALUE " +
  43. "('"+thisName+"', '"+thisGenre+"', '"+thisPrice+"')");
  44. }
  45.  
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
Add Comment
Please, Sign In to add comment