How to Connect Eclipse With MYSQL | Connecting MYSQL with Eclipse | 2024

Поделиться
HTML-код
  • Опубликовано: 24 сен 2024
  • In this Video, we look into the steps that need to be followed to successfully connect Eclipse with MYSQL.
    Links:
    1. dev.mysql.com/...
    2. download.eclip...
    3. download.eclip... (to paste in eclipse)
    Demo Program:
    import java.sql.*; // Using 'Connection', 'Statement' and 'ResultSet' classes in java.sql package
    public class jdbctest {
    // Save as "JdbcSelectTest.java"
    public static void main(String[] args) {
    try (
    // Step 1: Connect to the database via a 'Connection' object called 'conn'
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test", "root", "12345678#");
    //System.out.println("Connected");
    // Step 2: Construct a 'Statement' object called 'stmt' inside the Connection created
    Statement stmt = conn.createStatement();
    ) {
    // Step 3: Write a SQL query string. Execute the SQL query via the 'Statement'.
    // The query result is returned in a 'ResultSet' object called 'rset'.
    String strSelect = "select Regno,age from student";
    System.out.println("The SQL statement is: " + strSelect + "
    "); // Echo For debugging
    ResultSet rset = stmt.executeQuery(strSelect);
    // Step 4: Process the 'ResultSet' by scrolling the cursor forward via next().
    // For each row, retrieve the contents of the cells with getXxx(columnName).
    System.out.println("The records selected are:");
    int rowCount = 0;
    // Row-cursor initially positioned before the first row of the 'ResultSet'.
    // rset.next() inside the whole-loop repeatedly moves the cursor to the next row.
    // It returns false if no more rows.
    while(rset.next()) { // Repeatedly process each row
    String Regno = rset.getString("Regno"); // retrieve a 'String'-cell in the row
    int age = rset.getInt("age"); // retrieve a 'double'-cell in the row
    //int qty = rset.getInt("qty"); // retrieve a 'int'-cell in the row
    System.out.println(Regno+","+age);
    ++rowCount;
    }
    System.out.println("Total number of records = " + rowCount);
    } catch(SQLException ex) {
    ex.printStackTrace();
    } // Step 5: Close conn and stmt - Done automatically by try-with-resources (JDK 7)
    }
    }
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;
    // assume that conn is an already created JDBC connection (see previous examples)
    Statement stmt = null;
    ResultSet rs = null;
    try {
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT foo FROM bar");
    // or alternatively, if you don't know ahead of time that
    // the query will be a SELECT...
    if (stmt.execute("SELECT foo FROM bar")) {
    rs = stmt.getResultSet();
    }
    // Now do something with the ResultSet ....
    }
    catch (SQLException ex){
    // handle any errors
    System.out.println("SQLException: " + ex.getMessage());
    System.out.println("SQLState: " + ex.getSQLState());
    System.out.println("VendorError: " + ex.getErrorCode());
    }
    finally {
    // it is a good idea to release
    // resources in a finally{} block
    // in reverse-order of their creation
    // if they are no-longer needed
    if (rs != null) {
    try {
    rs.close();
    } catch (SQLException sqlEx) { } // ignore
    rs = null;
    }
    if (stmt != null) {
    try {
    stmt.close();
    } catch (SQLException sqlEx) { } // ignore
    stmt = null;
    }
    }

Комментарии •