Find us on Google+ Kill the code: Database Connectivity in Java

Wednesday, 1 August 2012

Database Connectivity in Java


This program is for database connectivity in java
follow the steps
1) first create the database in MS ACCESS (DATABASE + TABLE)
2) write a program and execute program
3) In program SagarDB is database name and DemoTable is table name in MS ACCESS.

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.*;

public class DemoDbconnect
{
public static void main (String[] args)
{
       try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn = DriverManager.getConnection("jdbc:odbc:SagarDB");
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("select * from DemoTable;");

while(rs.next())
{
System.out.println(rs.getString(1)+"   " + rs.getString(2)+"   " + rs.getString(3)+"   ");
}
cn.close();
       }
 

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

No comments:

Post a Comment