wood burning stoves 2.0*
The moose likes JDBC and the fly likes Help Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Help Please" Watch "Help Please" New topic
Author

Help Please

Namnai Kidorkar
Greenhorn

Joined: Jul 02, 2005
Posts: 25
I know if I search the net and spend time on it I will find the answers, but In a hurry right now and thinking of saving that time. If someone can help that will be really appreciated.

I only have a vague idea about database. All i used so far is borland's own style of database (.jds files). Eventhing is built into JBuilder compiler, Followed a tutorial to get a simple program that access a database. but now need to move with the free stuff and more mainstream flow. My question is, where do I start to have a simple java program that access a database (an .sql file i guess) and displays the results. I'm using netbeans compiler. I found a program, but it prints a catch "unable to load driver class". That means, I don't know how to handle drivers.

Please let me know where to start with it.

Do i have to learn sql or having a sql file is ok (does not matter for me what data is showing)
how do i set up driver and stuff.

If u know about a tutorial that matches my need, please post it here.

thanks in advanced.
[ July 16, 2005: Message edited by: Namnai Kidorkar ]
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
The link below may get you started:

JDBC Database Access
Namnai Kidorkar
Greenhorn

Joined: Jul 02, 2005
Posts: 25
Thanks Craig. I am going throw the tutorial, but confused about one thing.

I know they might sound silly or naive, still......

1) Is there such file called .sql file?

from the tutorial it seems that they are doing everything by code. How is this file created?

2) I just need the simplest possible form of a java program that shows the content of a database file. I don't really want to do anything with it at this moment.

Is there any simpler way to get it?

sorry, if i'm annoying you guys.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

A bit of terminology:
Firstly, the program managing a database is the RDBMS - Relational Database Management System. This is usually what you refer to when you say Oracle, MySQL, MS SQL etc. A DBMS manages databases, which are collections of tables of data and other stuff.

Creating and setting up actual databases and DBMS is usually out of scope in the forum, but if you have a specific question people will always be happy to help.

A common way to interact with a database is via SQL - Structured Query Language, which is just text. This is a simple way to put data in and get it out of the database. Since it is just text, you can put it in a text file and read it from a text file rather than writing the same thing every time. If you change the file's extension to '.sql' it is still a text file, it just contains SQL code.

To answer your question: given the pointers above, yes there is a type of file called 'sql', but it has no significance in Java, although it may mean something to the database you're using. I'm not sure what the SQL you're talking about is supposed to accomplish.

Dave
Namnai Kidorkar
Greenhorn

Joined: Jul 02, 2005
Posts: 25
Thanks Dav for reply, lets see if I can make the question more specific:

take the code below for example:

import java.sql.*;

public class SelectFromPer {

public static void main(String argv[]) {


try {

// Load the JDBC-ODBC bridge
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

// specify the ODBC data source's URL
String url = "jdbc dbc:SSPer";

// connect
Connection con = DriverManager.getConnection(url,"North","Ken");

// create and execute a SELECT
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("SELECT Surname,FirstName,Category FROM Per");

System.out.println("Class is SelectFromPer\n");
// traverse through results
System.out.println("Found row:");

while (rs.next()) {

// get current row values
String Surname = rs.getString(1);
String FirstName = rs.getString(2);
int Category = rs.getInt(3);

// print values
System.out.print (" Surname=" + Surname);
System.out.print (" FirstName=" + FirstName);
System.out.print (" Category=" + Category);
System.out.print(" \n");
}


// close statement and connection
stmt.close();
con.close();
} catch (java.lang.Exception ex) {

ex.printStackTrace();
}

}

}


Questions:

String url = "jdbc dbc:SSPer";

does this mean, i'm using a file named SSPer? SSPer.sql?

("SELECT Surname,FirstName,Category FROM Per");

Surname, FirstName are column's of a sql file? how do I know what column's are in a sql file?

I guess where I'm stuck now, is that I need a sql file with known username and password and known column names. In case, how to create such a file is out of the scope, is there any way to get a file where I know all those?

or something is wrong with my understanding?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You might get some help in this thread, but basically:

The Class.forName() line asks the driver to register itself with the DriverManager.
DriverManager.getConnection(url,...) asks the DriverManager to provide a connection. There are a few step here. First the DriverManager asks each of the Drivers registered if they know how to handle this type of URL. (this allows the JDBC framework to manage multiple Driver types at once). Then the DRiverManager asks the Driver to provide a Connection with the information from the JDBC URL.

The actual format of JDBC URLs and the information stored in them changes for each database, but the JDBC-ODBC bridge you ate using is in the form jdbc:odbc:<odbc data source name>

So it means it expects to find an ODBC source on the local machine called 'SSPer'. To create one, you can use an Access database and give the Access MDB file an ODBC name. Creating the tables and data in Access can be your job

To create an ODBC name for an Access database, open the Administrative tools, open 'data sources', select the 'File DSN' tab, locate your MDB file, select 'add', select the Access ODBC type, give it the ODBC name. This might be a bit wrong since I didn't actually do it on my machine, but it should get you started.

Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help Please
 
Similar Threads
help (getting started)
Connecting to a database
JDBC-ODBC Bridge VS JNDI
Connecting a Java app. to a MySql DB
where to put my jdbc driver to mysql?