Java JDBC Example - Basic Database Operation

This is a simple code that shows how to use JDBC. It shows how to select data from the northwind database running on localhost to which we connect using USERNAME/PASSWORD login. This is the basic database operation. INSERT/UPDATE/DELETE are even simpler since they don't require looping through the result set.

import java.sql.*;

public class JDBCDemo {
    public static void main( String[] args ) {
        try {
            // Connect to the database
            Class.forName("org.gjt.mm.mysql.Driver");
            String url = "jdbc:mysql://localhost/northwind";
            Connection con = DriverManager.getConnection(url, "USERNAME", "PASSWORD");
            // Execute the SQL statement
            Statement stmt = con.createStatement();
            ResultSet resultSet = stmt.executeQuery("SELECT * from customers");
            System.out.println("Got results!");
            // Loop thru all the rows
            while( resultSet.next() ) {
                String data = resultSet.getString( "CompanyName" );
                System.out.println( data );
            }
            stmt.close();
        }
        catch( Exception e ) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Related Marakana Courses

  • Fundamentals of Java Training
  • Advanced Java Training

7 Responses to “Java JDBC Example - Basic Database Operation”

  1. Naveen Kumar Singh Says:

    There is only a program that how to connect database with java but it has no decription or explanation how this is accomplished

  2. Asela Says:

    good

  3. Amoxicillin. Says:

    Amoxicillin….

    Diagram of amoxicillin. Amoxicillin. Safety of amoxicillin and greyhounds. Amoxicillin trihydrate. Amoxicillin no prescription. Dosing of amoxicillin for sinus infection….

  4. Narender Says:

    There is only a program that how to connect database with java but it has no decription or explanation how this is accomplished

  5. RV Says:

    There is no DESCRIPTION regarding the programwhat the fuck is this worst site i have ever seen fuck this site off

  6. CB Says:

    Basic and to the point. Good job!!!

  7. name Says:

    I have not my own website

Leave a Reply