Monday, December 27, 2010

JDBC with mysql

What are the pre-requirements for starting JDBC?


Mysql server can be started on windows using following command

NET START mysql

Mysql server can be stopped on windows using following command

NET STOP mysql

There is another way to start stop mysql on windows,right-click MyComputer manage-->Services and Applications-->Services find MySQL and toggle start stop or restart.

After downloading connector/j the mysql-connector-java-5.1.14-bin.jar file can be used as an external jar file to the JDBC project. Please refer previously written post about adding external jars for java or post for adding external jars in eclipse.

what is jdbc?

JDBC(java Database Connectivity) is Java application programming interface that allows the Java programmers to access database management system from Java code.

Creating a database with jdbc sample code

package pac1;

import java.io.*;
import java.sql.*;

public class CreateDatabase {
 public static void main(String[] args) {
  System.out.println("Database creation example!");
  Connection con = null;// Connection is an interface in java.sql package
        // that specifies connection with specific
        // database like MySql. The SQL statements are
        // executed within the context of the Connection
        // interface.
  try {
   Class.forName("com.mysql.jdbc.Driver");// this static method
             // attempts to load the
             // class and returns class
             // instance and takes string
             // type value (driver) after
             // that matches class with
             // given string
   // DriverManager is a class of java.sql package that controls a set
   // of JDBC drivers. Each driver has to be register with this class.
   con = DriverManager.getConnection("jdbc:mysql://localhost:3306",
     "root", "123");// establishes a connection to specified
         // database url
   try {
    // Statement an interface. Statement object executes the SQL
    // statement and returns the result it produces
    Statement st = con.createStatement();// method of Connection
              // interface. which
              // returns Statement
              // object. This method
              // will compile again
              // and again whenever
              // the program runs.
    BufferedReader bf = new BufferedReader(new InputStreamReader(
      System.in));
    System.out.println("Enter Database name:");
    String database = bf.readLine();
    st.executeUpdate("CREATE DATABASE " + database);// method also
                // executes SQL
                // statement
                // that may be
                // INSERT,
                // UPDATE OR
                // DELETE
                // statement are
                // used in the
                // code. It
                // takes string
                // types
                // parameters
                // for SQL
                // statement. It
                // returns int.
    System.out.println("1 row(s) affacted");
    con.close();// disconnecting from the connection, frees all the
       // resources occupied by the database.
   } catch (SQLException s) {
    System.out.print(s.getMessage());
    System.out.println("SQL statement is not executed!");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}



Connecting with a database sample code

import java.sql.*;

public class MysqlConnect{
  public static void main(String[] args) {
    System.out.println("MySQL Connect Example.");
    
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","root");
      System.out.println("Connected to the database");
      conn.close();
      System.out.println("Disconnected from database");
   
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}


Note that newly created databases get saved in C:\ProgramData\MySQL\MySQL Server 5.5\data.

No comments :

Post a Comment