Re: Problem while connecting to MySQL using JDBC
Posted by: John Eby
Date: March 12, 2007 01:35AM

Here is more information:

I am using
Fedora Core 6
Eclipse 3.2.2
MySQL 5.0.27
Java 1.4.2.0
MySQL-Connector 5.0.5 (same error for connector 3.1.14)

Here is the error message (with some other output from the program at first).
All Code listed below

Getting MySQLDriver ...


Connecting to Database ...

DB Done.

SQLException: Error during query: Unexpected Exception: java.io.CharConversionException message given: null

Nested Stack Trace:


** BEGIN NESTED EXCEPTION **

java.io.CharConversionException

STACKTRACE:

java.io.CharConversionException
at gnu.gcj.convert.Input_iconv.read(libgcj.so.7rh)
at java.lang.String.init(libgcj.so.7rh)
at java.lang.String.<init>(libgcj.so.7rh)
at com.mysql.jdbc.SingleByteCharsetConverter.<init>(SingleByteCharsetConverter.java:153)
at com.mysql.jdbc.SingleByteCharsetConverter.initCharset(SingleByteCharsetConverter.java:108)
at com.mysql.jdbc.SingleByteCharsetConverter.getInstance(SingleByteCharsetConverter.java:86)
at com.mysql.jdbc.Connection.getCharsetConverter(Connection.java:3471)
at com.mysql.jdbc.StringUtils.getBytes(StringUtils.java:609)
at com.mysql.jdbc.Buffer.writeStringNoNull(Buffer.java:655)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1678)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Connection.configureClientCharacterSet(Connection.java:2509)
at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:4096)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2756)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(libgcj.so.7rh)
at java.sql.DriverManager.getConnection(libgcj.so.7rh)
at DogsMainForm.WriteToDataBase(DogsMainForm.java:86)
at DogsMainForm$4.widgetSelected(DogsMainForm.java:209)
at org.eclipse.swt.widgets.TypedListener.handleEvent(org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar.so)
at org.eclipse.swt.widgets.EventTable.sendEvent(org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar.so)
at org.eclipse.swt.widgets.Widget.sendEvent(org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar.so)
at org.eclipse.swt.widgets.Display.runDeferredEvents(org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar.so)
at org.eclipse.swt.widgets.Display.readAndDispatch(org.eclipse.swt.gtk.linux.x86_3.2.2.v3236.jar.so)
at DogsMainForm.main(DogsMainForm.java:228)


** END NESTED EXCEPTION **


SQLState: S1000
VendorError: 0



The Code: MySQL connecton in function called WriteToDataBase()

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class DogsMainForm {
static Display display;
static Shell shell;
static Text dogName;
static Combo dogBreed;
static Canvas dogPhoto;
static Image dogImage;
static List categories;
static Text ownerName;
static Text ownerPhone;


public static void WriteToSystemOut() {

System.out.println("\nDog Name: " + dogName.getText());
System.out.println("Dog Breed: " + dogBreed.getText());
System.out.println("Owner Name: " + ownerName.getText());
System.out.println("Owner Phone: " + ownerPhone.getText());
System.out.println("Categories:");
String cats[] = categories.getSelection();
for (int i = 0; i < cats.length; i++) {
System.out.println("\t" + cats);
}
}

public static void WriteToFile() {
// Write to file
try {
System.out.println("Writing To File ...\n");
BufferedWriter dataFile = new BufferedWriter(new FileWriter("dogdata.txt",true));
dataFile.write("\nDog Name: " + dogName.getText() + "\n");
dataFile.write("Dog Breed: " + dogBreed.getText() + "\n");
dataFile.close();
System.out.println("File Done.\n");
} catch (IOException e) {
System.err.println(e);
} finally {
//
}
} // WriteToFile

// *********************************************************************

public static void WriteToDataBase() {
// Load the Java MySQL Driver
System.out.println("\nGetting MySQLDriver ...\n");
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
System.err.println(e + "\n");
}
System.out.println("\nConnecting to Database ... \n");
String url = "jdbc:mysql://localhost/DogDB?user=theuserremoved&password=thepassremoved";
Connection DBConnect;
try {
DBConnect = DriverManager.getConnection(url);

// do something

DBConnect.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
System.err.println("SQLState: " + ex.getSQLState());
System.err.println("VendorError: " + ex.getErrorCode());
}
System.out.println("DB Done.\n");
}
//********************************************************************************


public static void main(String[] args) {

display = new Display();
shell = new Shell(display);
shell.setText("Dog Show Entry");
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);

new Label(shell, SWT.NULL).setText("Dog's Name:");
dogName = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
dogName.setLayoutData(gridData);

new Label(shell, SWT.NULL).setText("Breed:");
dogBreed = new Combo(shell, SWT.NULL);
dogBreed.setItems(new String [] {"Collie", "Pitbull", "Poodle", "Scottie"});
dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));

Label label = new Label(shell, SWT.NULL);
label.setText("Categories");
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));

new Label(shell, SWT.NULL).setText("Photo:");
dogPhoto = new Canvas(shell, SWT.BORDER);
gridData = new GridData(GridData.FILL_BOTH);
gridData.widthHint = 80;
gridData.heightHint = 80;
gridData.verticalSpan = 3;
dogPhoto.setLayoutData(gridData);
dogPhoto.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (dogImage != null) {
event.gc.drawImage(dogImage, 0, 0);
}
}
});

categories = new List(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
categories.setItems(new String [] {
"Best of Breed", "Prettiest Female", "Handsomest Male",
"Best Dressed", "Fluffiest Ears", "Most Colors",
"Best Performer", "Loudest Bark", "Best Behaved",
"Prettiest Eyes", "Most Hair", "Longest Tail",
"Cutest Trick"});
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.verticalSpan = 4;
int listHeight = categories.getItemHeight() * 12;
Rectangle trim = categories.computeTrim(0, 0, 0, listHeight);
gridData.heightHint = trim.height;
categories.setLayoutData(gridData);

Button browse = new Button(shell, SWT.PUSH);
browse.setText("Browse...");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 5;
browse.setLayoutData(gridData);
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String fileName = new FileDialog(shell).open();
if (fileName != null) {
dogImage = new Image(display, fileName);
}
}
});

Button delete = new Button(shell, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (dogImage != null) {
dogImage.dispose();
dogImage = null;
dogPhoto.redraw();
}
}
});

Group ownerInfo = new Group(shell, SWT.NULL);
ownerInfo.setText("Owner Info");
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
ownerInfo.setLayout(gridLayout);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
ownerInfo.setLayoutData(gridData);

new Label(ownerInfo, SWT.NULL).setText("Name:");
ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

new Label(ownerInfo, SWT.NULL).setText("Phone:");
ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.horizontalSpan = 2;
enter.setLayoutData(gridData);
enter.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
//WriteToSystemOut();
// WriteToFile();
WriteToDataBase();
} // widgetSelected
});

Button resetButton = new Button(shell, SWT.PUSH);
resetButton.setText("Reset Form");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
gridData.horizontalSpan = 1;
resetButton.setLayoutData(gridData);
resetButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.pack();
}
});

shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}

if (dogImage != null) {
dogImage.dispose();
}

display.dispose();

} // main


} // dogsmainformclass

Options: ReplyQuote


Subject
Written By
Posted
Re: Problem while connecting to MySQL using JDBC
March 12, 2007 01:35AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.