Docstoc

jdbc connection in java

Document Sample
jdbc connection in  java Powered By Docstoc
					This sample Java program connects to MySQL databas
e using JDBC, executes a query and retrieves and p
rints the value of the database field.

This same sample code can be used to connect to an
y type of database, all you need to do is change t
he connection url (dbUrl in the sample). For this
code to work properly, you will need to download t
he mysql driver for JDBC in other words Java Conne
ctors from mysql.com site.

If after downloading the URL it still doesn't work
 then it is probably due to the classpath. You wil
l have to add the driver jar file in the classpath.
import java.sql.*;
import javax.sql.*;

public class jdbcdemo{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/y
ourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl
);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try
catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

}   //end main

}   //end class

				
DOCUMENT INFO
Shared By:
Stats:
views:9
posted:6/21/2012
language:English
pages:2
Description: this ebook show tow to connect java application and mysql data base using jdbc driver
Sameera Dissanayaka Sameera Dissanayaka Mr www.hackkat.blogspot.com
About Hi im Sameera Madushan Dissanayaka .Im undergraduate Student of University Of moratuwa .I love Programming Lnaguages Like Java,C,C++,C# i love to browse internet and love to animals