import java.sql.*;
/*********************************************************************************
Program to demonstrate connection to MS Access database using JDBC driver Type - I
Developed by : Malhar Vora
Developed on : 6-1-2011
Development Status : Completed
Email : vbmade2000@gmail.com
WebSite : http://malhar2010.blogspot.com
**********************************************************************************/
public class dbcon
{
public static void main(String[] args)
{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception E) {
System.err.println ("Unable to load driver.");
E.printStackTrace ();
}
try {
String db = "jdbc:odbc:db1";
conn = DriverManager.getConnection (db, "","");
st = conn.createStatement();
rs = st.executeQuery("Select * from emp");
while(rs.next())
{
System.out.println(rs.getString(1));
}
System.out.println ("*** Connected to the database ***");
}
catch (SQLException E) {
System.out.println ("SQLException: " + E.getMessage());
System.out.println ("SQLState: " + E.getSQLState());
System.out.println ("VendorError: " + E.getErrorCode());
}
try {
st.close();
if(conn!=null)
{
conn.close ();
}
}
catch (SQLException E) {
System.err.println ("Unable to load driver.");
E.printStackTrace ();
}
}
}
Saturday, February 5, 2011
Connect MS Access database using Type - I JDBC driver
Sunday, January 16, 2011
Using Hashtable in Java
import java.util.Hashtable;
import java.util.Enumeration;
public class HTDemo{
public static void main(String []str){
//Creating new Hatshtable
Hashtable ht = new Hashtable();
//Adding objects of Entry class to Hashtable
ht.put("Emma Watson",new Entry("Emma Watson","9741631235"));
ht.put("Hugh Jackman",new Entry("Hugh Jackman","7353421215"));
ht.put("Eric Bana",new Entry("Eric Bana","9471164719"));
ht.put("Steve Jobs",new Entry("Steve Jobs","9132135451"));
//Enumerating elements of Hashtable
displayElements(ht);
ht.remove("Eric Bana");
System.out.println("===============After removal==============");
displayElements(ht);
}
static void displayElements(Hashtable htable){
Enumeration e = htable.keys();
String key;
Entry ent;
while(e.hasMoreElements()){
key = (String)e.nextElement();
ent = (Entry)htable.get(key);
System.out.println("Name -> " + ent.getName());
System.out.println("Phone -> " + ent.getPhone());
System.out.println("-------------------------------");
}
}
}
class Entry{
private String name;
private String phone;
Entry(){
name="";
phone="";
}
Entry(String nm,String pn){
this.name = nm;
this.phone = pn;
}
String getName(){
return this.name;
}
String getPhone(){
return this.phone;
}
void setName(String nm){
this.name = nm;
}
void setPhone(String pn){
this.phone = pn;
}
}
Subscribe to:
Comments (Atom)