wood burning stoves 2.0*
The moose likes JDBC and the fly likes Need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Need help" Watch "Need help" New topic
Author

Need help

kevin jia
Ranch Hand

Joined: Jul 13, 2000
Posts: 38
Hi,
I'm new to JDBC, I create a simple database using MS Access, and I place this database on my local machine (drive A). I compiled a simple Java application as follow:
import java.sql.*;
import java.io.*;
class JDBCFirstExample {
public static void main(String[] args) {
Connection a_connection = null;
Statement a_statement = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.err.println("Cannot find databas driver " + e);
System.exit(1);
}
try {
a_connection = DriverManager.getConnection("jdbc dbc:JavatownZoo", "", "");
a_statement = a_connection.createStatement();
ResultSet rs = a_statement.executeQuery("SELECT * FROM Animals");
while (rs.next()) {
String x = rs.getString(1);
String y = rs.getString(2);
String z = rs.getString(3);
System.out.println(" Row " + x + " " + y + " " + z);
}
} catch (SQLException e) {
System.err.println("Error in database " + e);
System.exit(1);
}
}
}
I met this program also in drive A. I follow the standard steps add MS ODBC driver to my databae (start menu, setting, cotrolpanel, ODBC data source, User DSN, MS Access driver, add, finish, data source name (JavatownZoo).
When I run my application, always display "Error in database ...", and I foud a new file in drive A, JavatownZoo.ldb( I think it meas database lock). My database is very simple no problem(only two tables), and I don't set username and password.
Could anyone give me help, thanks very much.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
this example was orginially made to work with ms-access.
a few idea's:
1. verify you odbc setting one more time
2. add a userid and password to you database via odbc.
3. change: Class.forName
4. change: DriverManager.getConnection
5. change: selsql1

<pre>
// Proff of Concept
// Basic Example OF How JAVA1.2, JDBC2.0, DB2 Work Together
// sqlsel1 is a standard sql query
// sqlsel2 is a java stored procedure sql query
// Author: monty6 mireland1@mediaone.net
import java.sql.*; // java sql classes
public class JdbcTest {
public static void main(String args[]) {
try {
// load jdbc type 2 db2 driver
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

// con - connect driver manager // 1. manages all jdbc connection
// 2. driver manager: jdbc.db2
// 3. connection datebase: " " DSNT is a DB2 sub-system
// 4. userid: " " USERID
// 5. password: " " PASSWORD
Connection con = DriverManager.getConnection("jdbc:db2 SNT", "USERID", "PASSWORD");

// sql statement - create object to hold sql query
Statement stmt = con.createStatement();
// sql statement - define query
String sqlsel1 = "select * from dbname.tbname;";
// sql statement - execute query
ResultSet rs = stmt.executeQuery( sqlsel1 );
// sql statement - process query result set
while ( rs.next() ) {
String col1 = rs.getString("partition_nbr");
String col2 = rs.getString("as_of_date");
String col3 = rs.getString("reporting_date");
System.out.println( col1 + col2 + col3 );
} // end of while
} // end of try block

// load driver
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException!!!: " + e);
}
// sql sqlcode
catch(SQLWarning e) {
System.out.println("SQL_CODE !!!: " + e);
}

// sql class
catch(SQLException e) {
System.out.println("SQL_CLASS !!: " + e);
} // end of catch block(s)

} // end of main
} // end of class JdbcTest
</pre>
kevin jia
Ranch Hand

Joined: Jul 13, 2000
Posts: 38
Hi,
I only added one time ODBC driver to my database JavatownZoo.
I change the code for detail checking the exception and I added the password to my database, but it also display " Cannot connect to database...".
Could you give me any advice, thank you.
import java.sql.*;
import java.io.*;
import java.util.Date;
class JDBCFirstExample {
public static void main(String[] args) {
Connection a_connection = null;
Statement a_statement = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.err.println("Cannot find databas driver " + e);
System.exit(1);
}
Date start = new Date();
try {
a_connection = DriverManager.getConnection("jdbc dbc:JavatownZoo", "", "19981129");
} catch (SQLException e) {
System.err.println("Cannot connect to database " + e);
System.exit(1);
}
Date end = new Date();
long connectTime = start.getTime() - end.getTime();
System.out.println(connectTime);
try {
a_statement = a_connection.createStatement();
} catch (SQLException e) {
System.err.println("Cannot create statement " + e);
System.exit(1);
}
try {
ResultSet rs = a_statement.executeQuery("SELECT * FROM Animals");
while (rs.next()) {
String x = rs.getString(1);
String y = rs.getString(2);
String z = rs.getString(4);
System.out.println(" Row " + x + " " + y + " " + z);
}
} catch (SQLException e) {
System.err.println("Error in database " + e);
System.exit(1);
}
}
}
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I just created a new access database called 'mail'.
With a userid of 'userid' and a password of 'password'.
Also, within this database I have created a table called 'mail'.
The column layouts are:
name text 50
address text 50
city text 50
state text 50
zip text 50
* make the nessary odbc entries
** the following code should work without any changes.
<code>
import java.sql.*;// java sql classes
import java.io.*;
public class JdbcTest1 {
public static void main(String args[]) {
try {
// load jdbc type 3 odbc driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// con - connect driver manager
// 1. manages all jdbc connection
// 2. driver manager: jdbc dbc
// 3. connection datebase: " " mail
// 4. userid: " " USERID
// 5. password: " " PASSWORD
Connection con =
DriverManager.getConnection("jdbc dbc:mail", "USERID", "PASSWORD");
// sql statement - create object to hold sql query
Statement stmt = con.createStatement();
// sql statement - define query for table mail
String sqlsel1 = "select * from mail;";
// sql statement - execute query
ResultSet rs = stmt.executeQuery( sqlsel1 );
// sql statement - process query result set
while ( rs.next() ) {
String col1 = rs.getString(1);
String col2 = rs.getString(2);
String col3 = rs.getString(3);
System.out.println( col1 + col2 + col3 );
} // end of while
}// end of try block

// load driver
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException!!!: " + e);
}
// sql sqlcode
catch(SQLWarning e) {
System.out.println("SQL_CODE !!!: " + e);
}
// sql class
catch(SQLException e) {
System.out.println("SQL_CLASS !!: " + e);
}// end of catch block(s)

}// end of main
}// end of class JdbcTest1
</code>
Hope This Helps, Monty6

[This message has been edited by monty6 (edited August 07, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need help
 
Similar Threads
JTable & JDBC
no dafault driver specified
Populating Combo Box using one of the fields in oracle
NullPointerException
Writing data to a Database