• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting to an existing db

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic