Advertisement
ASP_Volume3 Databases/ JDBC #65498

DirectDB Direct Connection with Database in Java

(Please Vote Me) Make Direct Database Connection in Java without any DSN. Just give your Access .mdb Database file path and start using Database.

AI

Resumen de IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Código fuente
original-source
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());
			}
		}
	}
Comentarios originales (3)
Recuperado de Wayback Machine