Recent comments

Poll

Highest Users

UserPoints
shamali4560
Mujtaba1310
Billy650
maoo_o540
Moin_iyan360

Who's new

  • usman911
  • shshzaib
  • HafizSohaib
  • savitadhiya
  • i_feel_4u

Current weather

Lahore

day-clear
  • Clear sky
  • Temperature: 18 °C
  • Wind: South, 5.6 km/h
  • Pressure: 1016 hPa
  • Rel. Humidity: 37%
  • Visibility: 6 kilometers
Reported on:
Tue, 01/06/2009 - 13:30

Adsense

PerfSpot

Java DSN less Database Connectivity with MS Access

Aslam-O-Alaikum!
I wana share one of my best code while in 5th Semester in PUCIT, it is very simple and straight forward and self explained to any who has basic know how about java jdbc concepts. It gives you the flexibility to put your access db file with your .class file on any system and don't need to configue DSN at all, just runyour java code.

Here is the Code snippet and also .java source file is attached with this post.

//---------------------------------------------------------------------------------------------------

import java.sql.*;
    public class DirectDB{

            public static void main(String args[]){
                System.out.println("Starting");

                    try{
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        // set this to a MS Access DB you have on your machine
                        String filename = "z:/sis.mdb";
                        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
                        database+= filename.trim() + ";DriverID=22;READONLY=true}"; // add on to the end
                        // now we can get the connection from the DriverManager
                        Connection conn = DriverManager.getConnection( database ,"","");
                         // try and create a java.sql.Statement so we can run queries
                         Statement stmt = conn.createStatement();
                        ResultSet rs = stmt.executeQuery("SELECT * FROM table_name");
                        while(rs.next()){
                            //your business logic here.
                            }

                             }
                            catch(Exception ex){
                                System.out.println(ex.getMessage());
                                }
                            }
                        }

//---------------------------------------------------------------------------------------------------

Note: Attachement is java code, please remove the extra .txt extension or paste the code directly to your java editor to work properly.

 

PUCIT ONLINE Team: Punjab University College of Information Technology

Best Regards,
Faial Basra


AttachmentSize
DirectDB.java_.txt1.15 KB
Average: 2 (1 vote)
bitf05a018's picture

Nice Effort! I appericate to

Nice Effort!

I appericate to you for this effort and sharing.

I will be usefull for much more students of PUCIT.

Keep it up and try to share more informations regarding I.T. studies.

Thnx!

awaited's picture

hi.. this is good one but if

hi..

this is good one but if u say that it is ur best code than this is not good.:D..

becoz this is a very basic thing and evryone knows about the JDBC drivers,,;)..

and u did it in ur 5th semester..amazing....good..

Powered By SEO ignition: Internet Marketing Company