my dog learned polymorphism*
The moose likes JDBC and the fly likes Replacing name with first character in the DB (code included) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

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

vernon mweetwa
Greenhorn

Joined: Nov 26, 2004
Posts: 24
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" );
sqle.printStackTrace();
}
}

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

System.out.println("Hello! Vernon here");
try {
System.out.println(initializeConnection());
} 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
{
try
{
Class.forName( "com.mysql.jdbc.Driver" );
}
catch( ClassNotFoundException cnfe )
{
cnfe.printStackTrace();
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
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Vernon, please do not start a new topic for the same question. Continue any discussion in the original topic.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing name with first character in the DB (code included)