Results 1 to 3 of 3
Thread: Correct Driver for JDBC Derby
- 06-20-2012, 04:48 AM #1
Member
- Join Date
- Jun 2012
- Location
- Nova Scotia, Canada
- Posts
- 1
- Rep Power
- 0
Correct Driver for JDBC Derby
Hi all, I'm new to the forum. I didn't plan on jumping right in and asking for help : ( Thanks in advance for any help!
I'm starting my second year of web development in September, and I decided to work on a personal project over the summer.
I have Java experience and a bit of Oracle SQL but have never worked with a JDBC.
Here is my attempt to pull data:
Using Netbeans 7.1, I created a sample database with a few entries of liquor names (no username or password).
The table is called MIXES. Sorry If I omitted any pertinent information here.
I need help choosing and installing the appropriate driver for this jdbc (I think).
Java Code://imports import java.sql.Connection; import java.sql.Statement; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; public class JavaDBTest { // database URL static final String DATABASE_URL = "jdbc:derby://localhost:1527/liquor"; // launch the application public static void main(String[] args) { // manage the connection Connection connection = null; // query statament Statement statement = null; //manage the results ResultSet resultSet = null; //connect to database and query the database try { //establish connection to database connection = DriverManager.getConnection(DATABASE_URL, "", ""); //create Statement for querying databse statement = connection.createStatement(); //process query results ResultSetMetaData metaData = resultSet.getMetaData(); int numberOfColumns = metaData.getColumnCount(); System.out.println("Liquors in Database:\n"); for (int i=1; i<=numberOfColumns; i++) { System.out.printf("%-8s\t", metaData.getColumnName(i)); System.out.println(); }// end for while (resultSet.next()) { for (int i=1; i<=numberOfColumns; i++) { System.out.printf("%-8s\t", resultSet.getObject(i)); System.out.println(); } //end for } // end while } // end try catch (SQLException sqlException) { sqlException.printStackTrace(); } //end catch //ensure resultSet, statment and connection are closed finally { try { resultSet.close(); statement.close(); connection.close(); } //end try catch (Exception exception) { exception.printStackTrace(); } //end catch }//end finally }//end Main }//end Class JavaDBTest
- 06-20-2012, 05:57 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Correct Driver for JDBC Derby
Derby JDBC driver class can be found in the derby.jar file packaged with the database. Before create a connection to the database you'll need to register the driver class class so that it can be picked-up by the DriverManager to create a connection to the database. You can do it with the following snippet:
Java Code:... Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); ...Website: Learn Java by Examples
- 06-20-2012, 09:26 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
By santde in forum JDBCReplies: 0Last Post: 03-03-2012, 01:01 AM -
JDBC Problem - com.mysql.jdbc.Driver
By icu222much in forum Advanced JavaReplies: 5Last Post: 11-22-2011, 04:54 PM -
Runtime Exceptions for JDBC program using Derby Client in Netbeans
By kasiforjava in forum JDBCReplies: 1Last Post: 09-12-2011, 10:42 AM -
problem with derby driver
By vitaly87 in forum New To JavaReplies: 0Last Post: 07-16-2011, 01:47 PM -
JDBC Driver
By Ursula in forum New To JavaReplies: 6Last Post: 08-23-2010, 05:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks