Hi,
I'm new to java and i'm learning java. I'm doing programming in
jdbc. I have downloaded mysql-essential-5.0.22-win32.. language &
mysql-connector-java-3.1.13.., & i'm working in jdk1.5.0_06 version.
What i want to do is i should have to access the table which was
already created in mysql. My program is like this.
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 DisplayAuthors
{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/books";
// launch the application
public static void main( String args[] )
{
Connection connection = null; // manages connection
Statement statement = null; // query statement
// connect to database books and query database
try
{
Class.forName( JDBC_DRIVER ); // load database driver class
// establish connection to database
connection =
DriverManager.getConnection( DATABASE_URL, "jhtp6",
"jhtp6" );
// create Statement for querying database
statement = connection.createStatement();
// query database
ResultSet resultSet = statement.executeQuery(
"SELECT authorID, firstName, lastName FROM authors" );
// process query results
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println( "Authors Table of Books Database:" );
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", metaData.getColumnName( i )
);
System.out.println();
while ( resultSet.next() )
{
for ( int i = 1; i <= numberOfColumns; i++ )
System.out.printf( "%-8s\t", resultSet.getObject( i )
);
System.out.println();
} // end while
} // end try
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
System.exit( 1 );
} // end catch
catch ( ClassNotFoundException classNotFound )
{
classNotFound.printStackTrace();
System.exit( 1 );
} // end catch
finally // ensure statement and connection are closed properly
{
try
{
statement.close();
connection.close();
} // end try
catch ( Exception exception )
{
exception.printStackTrace();
System.exit( 1 );
} // end catch
} // end finally
} // end main
} // end class DisplayAuthors
Here my problem is how to connect to database url. I kept the connector
& mysql in the same root directory that with my java program. It was
compiled nicely, but when i run this programm i have got an exception
like class com.mysql not found. Please help me in connecting to
database url.
RedGrittyBrick - 12 Jun 2006 21:32 GMT
> Hi,
> I'm new to java and i'm learning java. I'm doing programming in
[quoted text clipped - 26 lines]
> {
> Class.forName( JDBC_DRIVER ); // load database driver class
<snip: remainder of program code>
> Here my problem is how to connect to database url. I kept the connector
> & mysql in the same root directory that with my java program. It was
> compiled nicely, but when i run this programm i have got an exception
> like class com.mysql not found.
It sounds as if your CLASSPATH environment variable does not include the
full path and filename of the .jar file containing the MySQL client
driver.
http://mindprod.com/jgloss/classpath.html
If you are running your program from an IDE such as Eclipse then you
should configure the IDE to include the MySQL .jar for your project.
If you have created a .jar for your app then a typical way to deploy the
app with the JDBC driver is to install the JDBC driver in a ./lib
subdirectory with a suitable reference in the manifest in your app's
.jar file. Google for details.