468,283 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,283 developers. It's quick & easy.

ODBC setting

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
0 4956

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Riccardo | last post: by
5 posts views Thread by SerGioGio | last post: by
3 posts views Thread by Lauren Quantrell | last post: by
reply views Thread by Julia Baresch | last post: by
4 posts views Thread by Szymon Dembek | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.