Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Development
java and jdbc
gwladys boy
hi guys
i am trying to use this java code to get access to a ms access database file called fred, stored in the root of the c: drive.
i get a sql error when i run it in jbuilder.?
any tips?
__________________________________________________________________
package jdbc;
import javax.swing.UIManager;
import sun.jdbc.odbc.*;
import java.sql.*;
import sun.jdbc.odbc.*;
public class JdbcTest {
public JdbcTest() {
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MS Access Database;DBQ=c:\\fred.mdb", "", "");
System.out.println("connected ok");
Statement stmt = con.createStatement();
// putting data into the database
String insertSQL="insert into Coffee (name, price, quantity, sales) values ('greg',12,12,5 )";
stmt.executeUpdate(insertSQL);
// getting data from the database
String selectSQL="select * from Coffee where sales < 9";
ResultSet rs = stmt.executeQuery(selectSQL);
while (rs.next()){
String name = rs.getString("name");
System.out.println(name);
}
}
catch(Exception ee)
{
System.out.println("ERROR connecting:"+ee);
}
}
public static void main(String[] args) {
JdbcTest jdbcTest1 = new JdbcTest();
}
}
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
Stark
You might want to post the error.
Hobbes
yea the full stack trace would help.
viking
You may need to create a DataSource Name (DSN) in the 32-bit ODBC control panel in order to connect to the Mdb file rather than using the absolute path the file (ie. c:\\fred.mdb)
e.g jdbc:odbc:<DSN>
viking
PS. I may be wrong, its been a while...
briano
I have a JSP that also uses an Ms Access DB.
When I am connecting to it I had to use the string:
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\\Apache\Tomcat\webapps\ass2\Shopping.mdb"
This works without a DSN
The only differences is the parenthisis and the spelling though.
Also, you should probably close the result set, statement and connection when you are finished with them.
HaVoC
i just did a project with jdbc and ms access
viking is right you need to crete a dsn in Data Sources (Odbc)
and use the name in the program
or at least that how i did it.
ie
Connection con ;
public void Open()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//"mydatabase" is name of ur dsn that u setup
con = DriverManager.getConnection("jdbc:odbc:mydatabase");
}
catch(Exception e)
{}
}