aspose file tools*
The moose likes JDBC and the fly likes Connecting to an existing db Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connecting to an existing db" Watch "Connecting to an existing db" New topic
Author

Connecting to an existing db

Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
I've created a MySQL database on my local workstation. Made a table and added some info and so forth. Now my question is how can I connect to that database with a JDBC connection? I'm having issues with the whole "JDBC driver" deal really. Do I need a specific one or something? At first, I used one found by going to Control Panel > Admin Tools > ODBC Admin. Under System DSN I created a new Driver do Microsoft dBase. Gave it a name and used that in the con code. It connected a created a new database for me in my project directory. Obviously that isn't correct. Can anyone help me out?
hob hartman
Greenhorn

Joined: Mar 04, 2002
Posts: 11
Chris,
I would go to MySQL.com and download their MySqlODBC 2.50 driver. Then after installing the driver, go to control panel and the ODBC administrator dialog and set up your DSN using the MySQL driver that will now show up in the list. You will specify your db, username, password, and of course your DSN name. This driver is not JDBC driver but connects up with Sun's JDBC-ODBC driver bridge. I offer this since you mentioned the ODBC settings in the control panel.
Then you are set to do your code and connect to your db. Here is a little of my code to make my connection:
Connection con =null;
//selecting the driver as the JdbcOdbc driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//create a connection to my MySql db using the DataSourceName in the ODBC
//dialog box in the Control Panel; my DSN is "WaltODBC", user is "", and password
//is "" Note: I downloaded the MySqlODBC 2.50 from MySQL web site
con=DriverManager.getConnection ("jdbc dbc:WaltODBC","","");
//create your statement object to carry your sql
Statement statement = con.createStatement();
//create your resultset;Human is a table in my db
ResultSet rs = statement.executeQuery(" SELECT * FROM Human");

hob
Chris Stewart
Ranch Hand

Joined: Sep 29, 2001
Posts: 124
Thanks alot for your help. I figured out that I just needed to add the jar file holding the ODBC driver to my classpath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting to an existing db
 
Similar Threads
connecting to database
Cannot lookup JNDI resource for MySql JDBC driver on Tomcat & Axis
Topspeed INSERT statement issue
Tomcat Connection Pooling Help required
Tutorial PostgreSQL 8.3 on Glassfish V2.1 for ConnectionPool in Netbeans 6.5.1, WindowsXP (Post 3)