Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to connect to jdbc

  Asked By: Jody    Date: Jun 01    Category: Java    Views: 651
  

It's about how to connect to jdbc. I use Websphere Studio Application
Developer (WSAD). Here i attach you the code:

import java.sql.*;
import java.io.*;

public class Listing {
// database table
static String dbtab = "animals";

public static void main(String[] args) {

System.out.println("Animals Listing for " + dbtab);

// connect to database
Connection con = null;
con = connect();

Statement stmt = null;
ResultSet rs = null;
String select = "SELECT * FROM itso." + dbtab;
try {
stmt = con.createStatement();
rs = stmt.executeQuery(select);
while (rs.next()) {
String name = rs.getString("name");
String family = rs.getString("family");
String location = rs.getString("location");
String feed = rs.getString("feed");
System.out.println(
name + " " + family + " " + location + " " + feed);
}
System.out.println("End of Listing");
stmt.close();
con.close();
} catch (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
}
}

protected static Connection connect() {
Connection con=null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
con = DriverManager.getConnection("jdbc:db2:javazoo");
} catch (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
}
return con;
}
}

This code is work properly. But, pay attention in this another code, which
is use the same string to connect to jdbc, but it doesn't work.

// JDBC with Exceptions

import java.sql.*;
import java.io.*;

class JDBCFirstExample
{
public static void main(String [] argv) {

Connection a_connection = null;
Statement a_statement = null;
try {

Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
<ERROR>
a_connection =
DriverManager.getConnection("jdbc:db2:JavaZoo","","");
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);
} catch (ClassNotFoundException e) {
System.err.println("Cannot find database driver " + e);
}

} // END OF main
} // END OF class JDBCFirstExample

Please see the <ERROR> tag! It raise error message : "Unhandled exception
type java.lang.IllegalAccessException". But, as you can see, it use the
same string with the code before.
Why is that happen ?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Sophia Campbell     Answered On: Jun 01

Can you replace your catch  statements so that they
look like this

} catch (SQLException e1) {
System.err.println("Error in database  " +
e1);
} catch (ClassNotFoundException e2) {
System.err.println("Cannot find  database
driver " + e2);
}

I am not 100 % sure that this may solve your
problem...but can you have a go at this..

 
Didn't find what you were looking for? Find more on how to connect to jdbc Or get search suggestion and latest updates.




Tagged: