VB icon

DirectDB Direct Connection with Database in Java

Email
Submitted on: 1/4/2015 7:22:00 PM
By: Faisal Basra (from psc cd)  
Level: Advanced
User Rating: By 6 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2546
 
     (Please Vote Me) Make Direct Database Connection in Java without any DSN. Just give your Access .mdb Database file path and start using Database.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: DirectDB Direct Connection with Database in Java
// Description:(Please Vote Me) Make Direct Database Connection in Java without any DSN. Just give your Access .mdb Database file path and start using Database.
// By: Faisal Basra (from psc cd)
//**************************************

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 con = DriverManager.getConnection( database ,"","");
			 // try and create a java.sql.Statement so we can run queries
			 Statement s = con.createStatement();
			 s.execute("create table profile ( userid integer )"); // create a table
			 s.execute("insert into profile values(1)"); // insert some data into the table
			 s.execute("select column_name from profile"); // select the data from the table
			 ResultSet rs = s.getResultSet(); // get any ResultSet that came from our query
			 if (rs != null) // if rs == null, then there is no ResultSet to view
			 while ( rs.next() ) // this will step through our data row-by-row
			{
			/* the next line will get the first column in our current row's ResultSet
			as a String ( getString( columnNumber) ) and output it to the screen */
			System.out.println("Data from column_name: " + rs.getString(1) );
			 }
			 //s.execute("drop table profile");
			 s.close(); // close the Statement to let the database know we're done with it
 con.close(); // close the Connection to let the database know we're done with it
			}
		catch(Exception ex){
			System.out.println(ex.getMessage());
			}
		}
	}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.