Source Code : Load MYSQL JDBC Driver and run the query

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Load MYSQL JDBC Driver and run the query



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HelloMySQLJDBC {
  Connection connection;

  private void displaySQLErrors(SQLException e) {
    System.out.println("SQLException: " + e.getMessage());
    System.out.println("SQLState:     " + e.getSQLState());
    System.out.println("VendorError:  " + e.getErrorCode());
  }

  public HelloMySQLJDBC() {
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
      System.err.println("Unable to find and load driver");
      System.exit(1);
    }
  }

  public void connectToDB() {
    try {
      connection = DriverManager.getConnection("jdbc:mysql://192.168.1.25/accounts?user=spider&password=spider");
    } catch (SQLException e) {
      displaySQLErrors(e);
    }
  }

  public void executeSQL() {
    try {
      Statement statement = connection.createStatement();

      ResultSet rs = statement.executeQuery("SELECT * FROM bool");

      while (rs.next()) {
        System.out.println(rs.getString("a") + " " + rs.getBoolean("a"));
        System.out.println(rs.getString("b") + " " + rs.getBoolean("b"));
        System.out.println(rs.getString("c") + " " + rs.getBoolean("c"));
        System.out.println(rs.getString("d") + " " + rs.getBoolean("d"));
      }

      rs.close();
      statement.close();
      connection.close();
    } catch (SQLException e) {
      displaySQLErrors(e);
    }
  }

  public static void main(String[] args) {
    HelloMySQLJDBC hello = new HelloMySQLJDBC();

    hello.connectToDB();
    hello.executeSQL();
  }
}


           
       

Thank with us