473,245 Members | 1,625 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,245 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 5182

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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.