JDBC

March 26, 2009 by bogdan

I will describe in this review the steps taken to create a class, after reading JDBC tutorial and issues arised.
I will describe the steps I’ve gone:

1) download JDK1.6.0_12  which include JavaDB that contains classes and interfaces required for development JDBC applications .

2) NetBeans download the latest version (by downloading the server Glash Fish prelude 1.3, this also include JavaDB). 

3) After installing NetBeans:

 Connecting to Derby database server and I created a database (from window - services - (under Databases) JavaDB - start the server,  (the Java DB drivers are displayed under JavaDB) -  (JavaDB - create database “ultima”)

4) I made a class that will create a table in the above database “ultima”.

I tried to load this drivers ClientDriver and EmbeddedDriver by below statements .

Class.forName ( “org.apache.derby.jdbc.ClientDriver”);

Class.forName ( “org.apache.derby.jdbc.ClientDriver”);

I gave run time error “ClassNotFoundException” for drivers mentioned above.

I do this:

If that drivers are not added in NetBeans make following: Tools-Libraries - to create a new library (EmbeddedDriver) that adds derby.jar files from the specified location (Sun - JavaDB - ..).

When you create the application right click on the symbol applications - project proprietes - libraries - add the drivers, and then the drivers will be recognized by the class.

This is the class wich I tried to run:
package javadbdemo;
import java.sql.*;
/**
 *
 * @author boghy
 */
public class NewClass1 {
 public static void main (String[] args)
    {
        String url = “jdbc:derby://localhost:1527/ultima”;
        Connection con;
        String createString;
        createString = “create table cafele” + (”NumeCafea varchar(32),” + “SupID Integer, “+ “Pret float,” + “vanzari integer,” + “total integer”);
        Statement stmt;

        try{           

Class.forName(”org.apache.derby.jdbc.ClientDriver”);
        }
            catch(java.lang.ClassNotFoundException e)
            {
                System.err.print(”ClassNotFoundException: “);
                System.err.print(e.getMessage());
            }
        try{
            con = DriverManager.getConnection(url);
            stmt = con.createStatement();
            stmt.executeUpdate(createString);
            stmt.close();
            con.close();
        }catch(SQLException ex)
        {      System.err.println(”SQLException:”+ ex.getMessage());

    }
        }
}

You must be logged in to post a comment.