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.
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
September 27th, 2007 at 3:11 am
There is only a program that how to connect database with java but it has no decription or explanation how this is accomplished
November 6th, 2007 at 7:57 pm
good
January 16th, 2009 at 7:22 am
Amoxicillin….
Diagram of amoxicillin. Amoxicillin. Safety of amoxicillin and greyhounds. Amoxicillin trihydrate. Amoxicillin no prescription. Dosing of amoxicillin for sinus infection….
March 24th, 2009 at 6:16 pm
There is only a program that how to connect database with java but it has no decription or explanation how this is accomplished
May 5th, 2009 at 1:25 am
There is no DESCRIPTION regarding the programwhat the fuck is this worst site i have ever seen fuck this site off
August 26th, 2009 at 1:36 pm
Basic and to the point. Good job!!!
January 6th, 2010 at 1:48 pm
I have not my own website