Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to an existing db

 
Chris Stewart
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot for your help. I figured out that I just needed to add the jar file holding the ODBC driver to my classpath.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic