Tuesday, 20 March 2012

Java program to connect to a database

Anyone of Java programmers have heard of JDBC. Today I am going to discuss some easiest ways of connecting to a database usin JDBC. JDBC actually stands for Java Database Connectivity. To use this technique you need JDBC drivers first of all to run the java codes. You can use Sun's JDBC-ODBC bridge by using Sun Microsystem's JDBC driver available with easysoft. But I prefer to use jstels JDBC-MDB driver(for connceting to MSAccess database files)as it does not require any installations. I will discuss my codebased on it.We wull need SQL codes to execute the Queries. You need to do the following :
1 : import java.sql.*;
2 : Create a Connection object like this ->
Class.forName("jstels.jdbc.mdb.MDBDriver2");  //loads class files of driver
Connection c=DriverManager.getConnection("jdbc:jstels:mdb:record.mdb");
3 :  Next create a Statement object like  ->
     Statement stmt=c.createStatement();
SQL Writing follow steps below ->
1 : stmt.execute("CREATE TABLE Records(Roll LONG, Sname STRING(50))"); //for creating table having the coloumns as described
2 : stmt.execute("INSERT INTO Records(Roll, Sname) VALUES(100860,'Malinga')");   //inserting values in fields

Related post and read after this --> Java program to connect to a database Contd.


  1. Its working fine. But how can I modify or delete or search and move through records.

  2. Very soon I will post all other features that can be done using JDBC part by part