Guest User

Untitled

a guest
Sep 4th, 2018
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. package try_1;
  2. import java.sql.*;
  3.  
  4. /**
  5. *
  6. * @author Anton
  7. */
  8.  
  9. public class Main {
  10.  
  11. /**
  12. * @param args the command line arguments
  13. */
  14. public static void main(String[] args)
  15. {
  16. try {
  17. try {
  18. // Establishing connection
  19. Class.forName("com.mysql.jdbc.Driver");
  20. Connection conn = DriverManager.getConnection(
  21. "jdbc:mysql://localhost:3306/test_db", "root", "");
  22.  
  23. helper.pIt("Connection_successful.");
  24. Statement stmt = conn.createStatement();
  25.  
  26. //CREATION
  27.  
  28. stmt.executeUpdate("CREATE TABLE test_db.Cookoo ("
  29. + "id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,"
  30. + "title VARCHAR(100),"
  31. + "info VARCHAR(1000),"
  32. + "PRIMARY KEY (id)"
  33. + ");");
  34.  
  35. helper.pIt("Database created successfully...");
  36.  
  37. try {
  38.  
  39. //INSERTION
  40. String name = "заголовок";
  41. String inf = "опейсание";
  42.  
  43. stmt.executeUpdate("INSERT INTO Cookoo VALUES (1, '" + name + "', '" + inf + "');");
  44. stmt.executeUpdate("INSERT INTO Cookoo VALUES (3, '" + name + "', '" + inf + "');");
  45. stmt.executeUpdate("INSERT INTO Cookoo VALUES (2, '" + name + "', '" + inf + "');");
  46.  
  47. //SELECTING + OUTPUT
  48. ResultSet rs = null;
  49. rs = stmt.executeQuery("SELECT * FROM Cookoo WHERE title LIKE '%гол%'");
  50.  
  51. helper.pIt("*");
  52. int id;
  53. String title;
  54. String info;
  55.  
  56. while (rs.next()) {
  57. title = rs.getString("title");
  58. id = rs.getInt("id");
  59. info = rs.getString("info");
  60. helper.pIt(" | " + id + " | " + title + " | " + info + " | ");
  61. //helper.pIt("*");
  62. }
  63.  
  64. rs.close();
  65. } catch(Exception a) {}
  66.  
  67. //DEATH = drop table
  68.  
  69. stmt.executeUpdate("DROP TABLE Cookoo");
  70. helper.pIt("Table killed...");
  71.  
  72. stmt.close(); conn.close();
  73.  
  74. } catch(ClassNotFoundException e) {
  75. helper.pIt("class not found, too bad");
  76. return;
  77. }
  78. } catch(SQLException x) {
  79. helper.pIt("fail with sql");
  80. // x.printStackTrace();
  81. return;
  82. }
  83. helper.pIt("Done.");
  84. return;
  85. }
  86. }
  87.  
  88. /**
  89. * helper is a cool class with my static utils
  90. * for example pIt is output
  91. */
Add Comment
Please, Sign In to add comment