By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

ODBC setting

P: n/a
ODBC

under System DSN

Setup Access Driver

give it the DSN (Data Source Name) MSA

Click on Select and point to the myfile.mdb (your database file)

click OK

then use this "OBJECT"

use the "insert" method

hope you know a little SQL or have a reference

-----------------------------------------------

import java.sql.*;

import java.net.URL;

class GrabTable {

public GrabTable (String URL, String Id, String Pw, String Table){

// Need to setup ODBC MS-Access Driver (named MSA)

url = URL;

table = Table; // Initialize Class Fields

pw = Pw;

id = Id;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

url = "jdbc:odbc:MSA";

Connection con = DriverManager.getConnection(url, id, pw);

con.close();

}

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

System.out.println("ODBC Connection (network), Password, or URL Problem!");

System.exit(0);

}
} // End Constructor

// Methods...

public void insert (String cols, String vals) {
//***************************
// vals = " 'Sean', 'Anderson', '513-722-0122' "

// cols = " FIRST, LAST, PHONE"

if (cols != "")

cols = " (" + cols + ")";

try {

Connection con = DriverManager.getConnection(url, id, pw);

Statement stmt = con.createStatement ();

stmt.executeUpdate("INSERT INTO "+ table + cols +" VALUES ("+vals+")");

// String Tested**

stmt.close();

con.close();

}

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

System.out.println("*insert* ODBC Problem!");

System.exit(0);

}
}


public int update (String set, String where) {

// e.g.

// set = " FIRST = 'Sean', LAST='Anderson' "

// where = " FIRST = 'Shawn', LAST = "Andersen", PHONE = '513-722-0122' "
int rowsUpdated=0;

try {

Connection con = DriverManager.getConnection(url, id, pw);

Statement stmt = con.createStatement ();

stmt.executeUpdate("UPDATE "+ table +" SET "+ set + " WHERE "+where);

// String tested**

stmt.close();

con.close();

}

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

System.out.println("*update* ODBC Problem!");

System.exit(0);

}

return rowsUpdated;
}

public ResultSet query (String cols) {
// Perform query and return in a RS

// Great for stuffing a table into a JTable ?
// cols = "FIRST, LAST" or " * " !

ResultSet rs=null;

try {

Connection con = DriverManager.getConnection(url, id, pw);

Statement stmt = con.createStatement ();

rs = stmt.executeQuery("SELECT "+ cols +" FROM "+table);

// String tested***

stmt.close();

con.close();

return rs;

}

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

System.out.println("*query* ODBC Problem!");

System.exit(0);

}
return rs;

}

public ResultSet whereQuery (String cols, String where) {
// Perform query and return in a RS

// Great for stuffing a table into a JTable ?
// cols = "FIRST, LAST" or " * " !

// where = "PHONE = '513-722-0122' "
ResultSet rs=null;

try {

Connection con = DriverManager.getConnection(url, id, pw);

Statement stmt = con.createStatement ();

rs = stmt.executeQuery("SELECT "+ cols +" FROM "+table+" WHERE "+where);

// String tested**

stmt.close();

con.close();

return rs;

}

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

System.out.println("*whereQuery* ODBC Problem!");

System.exit(0);

}

return rs;

}

public int findKey (String keyCol, String cols, String where) {

ResultSet rs=null;
// cols = "FIRST, LAST" or " * " !

// keyCol = "AUTHOR";

// where = "PHONE = '513-722-0122' "

try {

Connection con = DriverManager.getConnection(url, id, pw);

Statement stmt = con.createStatement ();
// **DEBUG** // - System.out.println("SELECT " + keyCol +", "+cols +" FROM
"+table+" WHERE "+where);
//rs = whereQuery(keyCol +", "+cols, where);

rs = stmt.executeQuery("SELECT " + keyCol +", "+cols +" FROM "+table+" WHERE
"+where);
if (!rs.next()){ stmt.close(); con.close(); return 0;}

else { int k = rs.getInt(keyCol); return k; }
}

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

System.out.println("*findKey* ODBC Problem!");

System.exit(0);

}


return -1; // If you get here there is a DATA PROBLEM

}

//Fields...

private String url;

private String table;

private String id;

private String pw;

} //End Class


--
DO NOT REPLY TO THIS MESSAGE
THE ADDRESS THAT SHOWS FOR YOU TO REPLY TO IS WRONG PURPOSFULLY!
THIS PREVENTS WEBSITES FROM GRABBING MY ADDRESS THERFORE REDUCING SPAM


Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.