Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.sql.*;
- /*
- 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:
- javac -cp "mysql-connector-java-5.1.18-bin.jar;." *.java
- To run on windows run the following in the command line:
- java -cp "mysql-connector-java-5.1.18-bin.jar;." jdbcTest
- On mac and Linux replace the ";" with a ":"
- Remember to fill in your own password and insert the port number that your mysql db is located on
- */
- public class jdbcTest {
- public static void main(String[] args) {
- String url = "jdbc:mysql://localhost:3306/";
- String dbName = "imdb";
- String driver = "com.mysql.jdbc.Driver";
- String userName = "root";
- String password = "kober111";
- try {
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url + dbName, userName, password);
- Statement st = conn.createStatement();
- System.out.println("Connected to MySQL");
- torture(st);
- conn.close();
- System.out.println("Disconnected from MySQL");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void oldMethod(Statement st) {
- try {
- String userAttribute = JOptionPane.showInputDialog("Attribute");
- String table = JOptionPane.showInputDialog("Table");
- String condition = JOptionPane.showInputDialog("Condition");
- ResultSet rs = st.executeQuery("SELECT" + " " + userAttribute + " " + "FROM" + " " + table + " " + "WHERE" + " " + condition);
- int successCount = 0;
- while (rs.next()) {
- successCount++;
- }
- System.out.println("Found: " + successCount + " number of conditions that was fulfilled by" + condition);
- /**
- ResultSet rs = st.executeQuery("SELECT gender, count(*) FROM person GROUP BY gender");
- while (rs.next()) {
- System.out.println(rs.getString("gender")+": "+rs.getInt(2));
- }
- **/
- st.executeUpdate("DROP TABLE IF EXISTS JDBCtest");
- st.executeUpdate("CREATE TABLE JDBCtest(id int, string varchar(10))");
- st.executeUpdate("INSERT INTO JDBCtest VALUES (1,\"Tada!\")");
- } catch (SQLException s) {
- System.out.println(s.toString());
- }
- }
- public static void torture(Statement st) {
- try {
- String numberStr = JOptionPane.showInputDialog("torture number");
- int number = Integer.parseInt(numberStr);
- st.executeUpdate("DROP TABLE IF EXISTS torture");
- st.executeUpdate("CREATE TABLE torture (id int)");
- for(int i = 0; i < number; i++ ) {
- st.executeUpdate("INSERT INTO torture VALUES (" +i+ ")");
- }
- for (int i = 0; i < number; i++) {
- 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");
- }
- /**
- ResultSet rs = st.executeQuery("SELECT gender, count(*) FROM person GROUP BY gender");
- while (rs.next()) {
- System.out.println(rs.getString("gender")+": "+rs.getInt(2));
- }
- **/
- } catch (SQLException s) {
- System.out.println(s.toString());
- }
- }
- }
- /*
- JDBC syntax examples:
- conn.setAutoCommit(false); // Disable automatic commit after each update
- conn.commit(); // Commit all pending updates
- conn.rollback(); // Abort all pending updates
- PreparedStatement insertPerson =
- conn.prepareStatement("INSERT INTO person VALUES (?,?,?,?,?,?)"); // Create prepared statement
- insertPerson.setInt(1, 123456);
- insertPerson.setString(2, "John Doe");
- insertPerson.setString(3, "M");
- insertPerson.setDate(4, new java.sql.Date(160617600000)); // Set date, given in miliseconds since 1970-01-01
- insertPerson.setNull(6,java.sql.Types.INTEGER); // Set to NULL
- insertPerson.executeUpdate(); // Execute prepared statement with current parameters
- Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
- stmt.setFetchSize(Integer.MIN_VALUE); // Make sure result is read incrementally from MySQL
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement