Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
    Bookmark Topic Watch Topic
  • New Topic

Replacing name with first character in the DB (code included)

vernon mweetwa
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi all

the following is code which needs to be factored so that the main method,when executed, the program can connect to the database known as art_server,look at all the firstnames in the database and replace them with their first character e.g name like vernon should finally appear as v.this type of mechanism should continue until all the records are dealt with.

As the code stands its able to print the statement "Hello! Vernon here" as well as "ERROR! DRIVER NOT FOUND. SYSTEM".

i am unable to call within the main method, the Change method as well as the prepared statement method so that the driver can be found for connectivity pyurposes and thereafter query the database for updates.

how do i go about it. thanks

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.DriverManager;

* Created by IntelliJ IDEA.
* User: vernon
* Date: Jan 14, 2005
* Time: 11:15:36 AM
* To Change this template use File | Settings | File Templates.
public class Change {
static String dbURLPrefixServer;
static String dbURLSuffixServer;
static String dbLocationServer;
static Connection dbConnectionServer;

static PreparedStatement changeFirstname;
PreparedStatement changeSurname;

public Change(String user, String password, String location)
dbURLPrefixServer = "jdbc:mysql://";
dbURLSuffixServer = "/art_server?user=" + user + "&password=" + password;
dbLocationServer = location;

try {
Connection con = initializeConnection();
catch( SQLException sqle )
System.out.println( "\n\nChange - Failed to connect to DB\n" );

public static void main(String[] args)
System.out.println( change() );

System.out.println("Hello! Vernon here");
try {
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.

private static char change() {
return 0;

private static Connection initializeConnection() throws SQLException
Class.forName( "com.mysql.jdbc.Driver" );
catch( ClassNotFoundException cnfe )
System.out.print( "\n\nERROR! DRIVER NOT FOUND. SYSTEM EXIT." );
System.exit( 0 );
System.out.println( "Attempting to connect to server " + dbLocationServer + ".....");
dbConnectionServer = DriverManager.getConnection( dbURLPrefixServer + dbLocationServer + dbURLSuffixServer );
System.out.println( "Connection established.");
return dbConnectionServer;

private static void prepareStatements( Connection dbServerConnection ) throws SQLException
System.out.println( "\nPreparing Statements.....");

changeFirstname = dbServerConnection.prepareStatement(
"SELECT firstname from students " );


[ January 18, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Posts: 65660
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Vernon, please do not start a new topic for the same question. Continue any discussion in the original topic.
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!