Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.sql.*;
  3.  
  4. /*
  5. To Compile on windows, place mysql-connector-java-5.1.18-bin.jar in the same directory as the java file and run the following in the command line:
  6. javac -cp "mysql-connector-java-5.1.18-bin.jar;." *.java
  7.  
  8. To run on windows run the following in the command line:
  9. java -cp "mysql-connector-java-5.1.18-bin.jar;." jdbcTest
  10.  
  11. On mac and Linux replace the ";" with a ":"
  12.  
  13. Remember to fill in your own password and insert the port number that your mysql db is located on
  14. */
  15. public class jdbcTest {
  16. public static void main(String[] args) {
  17. String url = "jdbc:mysql://localhost:3306/";
  18. String dbName = "imdb";
  19. String driver = "com.mysql.jdbc.Driver";
  20. String userName = "root";
  21. String password = "kober111";
  22.  
  23. try {
  24. Class.forName(driver);
  25. Connection conn = DriverManager.getConnection(url + dbName, userName, password);
  26. Statement st = conn.createStatement();
  27. System.out.println("Connected to MySQL");
  28. torture(st);
  29.  
  30. conn.close();
  31. System.out.println("Disconnected from MySQL");
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public static void oldMethod(Statement st) {
  38. try {
  39. String userAttribute = JOptionPane.showInputDialog("Attribute");
  40. String table = JOptionPane.showInputDialog("Table");
  41. String condition = JOptionPane.showInputDialog("Condition");
  42. ResultSet rs = st.executeQuery("SELECT" + " " + userAttribute + " " + "FROM" + " " + table + " " + "WHERE" + " " + condition);
  43. int successCount = 0;
  44. while (rs.next()) {
  45. successCount++;
  46. }
  47. System.out.println("Found: " + successCount + " number of conditions that was fulfilled by" + condition);
  48. /**
  49. ResultSet rs = st.executeQuery("SELECT gender, count(*) FROM person GROUP BY gender");
  50. while (rs.next()) {
  51. System.out.println(rs.getString("gender")+": "+rs.getInt(2));
  52. }
  53. **/
  54. st.executeUpdate("DROP TABLE IF EXISTS JDBCtest");
  55. st.executeUpdate("CREATE TABLE JDBCtest(id int, string varchar(10))");
  56. st.executeUpdate("INSERT INTO JDBCtest VALUES (1,\"Tada!\")");
  57. } catch (SQLException s) {
  58. System.out.println(s.toString());
  59. }
  60. }
  61. public static void torture(Statement st) {
  62. try {
  63. String numberStr = JOptionPane.showInputDialog("torture number");
  64. int number = Integer.parseInt(numberStr);
  65. st.executeUpdate("DROP TABLE IF EXISTS torture");
  66. st.executeUpdate("CREATE TABLE torture (id int)");
  67. for(int i = 0; i < number; i++ ) {
  68. st.executeUpdate("INSERT INTO torture VALUES (" +i+ ")");
  69. }
  70. for (int i = 0; i < number; i++) {
  71. ResultSet rs = st.executeQuery("SELECT count(*) FROM torture t1, torture t2, torture t3 WHERE t1.id<=t2.id AND t2.id<=t3.id AND t3.id<=t1.id");
  72. }
  73.  
  74. /**
  75. ResultSet rs = st.executeQuery("SELECT gender, count(*) FROM person GROUP BY gender");
  76. while (rs.next()) {
  77. System.out.println(rs.getString("gender")+": "+rs.getInt(2));
  78. }
  79. **/
  80. } catch (SQLException s) {
  81. System.out.println(s.toString());
  82. }
  83. }
  84. }
  85.  
  86. /*
  87. JDBC syntax examples:
  88.  
  89. conn.setAutoCommit(false); // Disable automatic commit after each update
  90. conn.commit(); // Commit all pending updates
  91. conn.rollback(); // Abort all pending updates
  92.  
  93. PreparedStatement insertPerson =
  94. conn.prepareStatement("INSERT INTO person VALUES (?,?,?,?,?,?)"); // Create prepared statement
  95. insertPerson.setInt(1, 123456);
  96. insertPerson.setString(2, "John Doe");
  97. insertPerson.setString(3, "M");
  98. insertPerson.setDate(4, new java.sql.Date(160617600000)); // Set date, given in miliseconds since 1970-01-01
  99. insertPerson.setNull(6,java.sql.Types.INTEGER); // Set to NULL
  100. insertPerson.executeUpdate(); // Execute prepared statement with current parameters
  101.  
  102. Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
  103. stmt.setFetchSize(Integer.MIN_VALUE); // Make sure result is read incrementally from MySQL
  104. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement