473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 5184

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Riccardo | last post by:
Hi, This jsp code doesn't run. Why ? ----------------------------------------------------------- <%@ page import="java.sql.*" %> <%! String Query(String SQL, String DSN) {
1
by: simianphile | last post by:
OK, I had a problem that I've now fixed but I can't really understand what was causing it in the first place. I have an intranet site that uses basic authentication to allow users to view and...
5
by: SerGioGio | last post by:
Hello, I am going nuts. I am trying to connect to my local ORACLE instance using ODBC. It used to work few weeks ago, but it fails now. Connection with: - SQL*plus: connection works! -...
3
by: Lauren Quantrell | last post by:
Maybe a dumb question - I'm new to ODBC. How do I install an Access ..mde file on a user's workstation and create the ODBC connection to the backend SQL Server database without having to go through...
0
by: Julia Baresch | last post by:
Everyone, I posted the message below back in February and didn't get any information. I also wasn't able to find any documentation in MS Help, web site, or other groups. I'm posting now with...
2
by: Crazy Cat | last post by:
Hi all, I am having trouble getting linked Oracle 9 server in MS SQL Server 2005 Express to work properly. My machine is running Windows XP. The Microsoft and Oracle OLE DB Providers have...
3
by: nolanmadson | last post by:
I'm creating a front-end for some Teradata user maintenance tables in MS Access. I've started having problems in occasionally not being able to insert or update records in these tables. I've been...
4
by: Szymon Dembek | last post by:
Hi Recently I did some DB2 and ODBC coding in Visual FoxPro. I bumped on a problem I cannot resolve. When I issue a delete statement that deletes no rows (no rows qualify for the WHERE...
1
by: fperri | last post by:
Hi, I am trying to set up an ODBC Connection on all of my workstations to an SQL Server (Developer Edition) database so that it can be used in Microsoft Access. I have had no problems with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.