kostenloser Webspace werbefrei: lima-city


MySQL-Java Problem

lima-cityForumProgrammiersprachenJava

  1. Autor dieses Themas

    ork

    ork hat kostenlosen Webspace.

    Hallo Leute,

    ich habe eine Klasse erstellt, die eine eingehende Nachicht in eine Datenbank schreiben soll. Leider liefert sie folgende Ausgabe:


    java.sql.SQLException: Unexpected exception encountered during query.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2593)
    at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1768)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3444)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2062)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(libgcj.so.81)
    at java.sql.DriverManager.getConnection(libgcj.so.81)
    at org.silix.chat.server.util.MessageTributer.connect(MessageTributer.java:93)
    at org.silix.chat.server.util.MessageTributer.<init>(MessageTributer.java:69)
    at org.silix.chat.server.util.MessageTributer.main(MessageTributer.java:195)
    Caused by: java.io.CharConversionException
    at gnu.gcj.convert.Input_iconv.read(libgcj.so.81)
    at java.lang.String.init(libgcj.so.81)
    at java.lang.String.<init>(libgcj.so.81)
    at com.mysql.jdbc.SingleByteCharsetConverter.<init>(SingleByteCharsetConverter.java:152)
    at com.mysql.jdbc.SingleByteCharsetConverter.initCharset(SingleByteCharsetConverter.java:107)
    at com.mysql.jdbc.SingleByteCharsetConverter.getInstance(SingleByteCharsetConverter.java:85)
    at com.mysql.jdbc.ConnectionImpl.getCharsetConverter(ConnectionImpl.java:2773)
    at com.mysql.jdbc.StringUtils.getBytes(StringUtils.java:679)
    at com.mysql.jdbc.Buffer.writeStringNoNull(Buffer.java:663)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2049)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
    ...11 more
    Exception in thread "main" java.lang.NullPointerException
    at org.silix.chat.server.util.MessageTributer.insertMessage(MessageTributer.java:127)
    at org.silix.chat.server.util.MessageTributer.main(MessageTributer.java:199)


    Hier der Code

    package org.silix.chat.server.util;
    
    /**
     * <tt>MessageTributer</tt> is a class which
     * deals with the inserted messages.
     */
    
    import org.silix.network.Package;
    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.SQLException;
    import java.sql.DriverManager;
    
    public class MessageTributer {
    	
    	/**
    	 * The host of the mysql-server.
    	 */
    	private String host;
    	
    	/**
    	 * The username of the mysql-server.
    	 */
    	private String user;
    	
    	/**
    	 * The password of the mysql-server.
    	 */
    	private String password;
    	
    	/**
    	 * The database of the mysql-server.
    	 */
    	private String database;
    	
    	/**
    	 * The connection of the mysql-server.
    	 */
    	private Connection con;
    	
    	/**
    	 * The statement of the mysql-connection.
    	 */
    	private Statement stmt;
    	
    	/**
    	 * Builds an object of MessageTributer.
    	 */
    	public MessageTributer() {
    		this("localhost", "root", "", "default");
    	}
    	
    	/**
    	 * Builds an object of MessageTributer.
    	 * @param host The host of the mysql-server.
    	 * @param user The user of the mysql-server.
    	 * @param password The password of the mysql-server.
    	 * @param database The database of the mysql-server.
    	 */
    	public MessageTributer(String host, String user,
    			String password, String database) {
    		this.host = host;
    		this.user = user;
    		this.password = password;
    		this.database = database;
    		
    		try {
    			loadDriver();
    			connect();
    		}
    		catch(ClassNotFoundException cnfe) {
    			cnfe.printStackTrace();
    		}
    		catch(SQLException sqle) {
    			sqle.printStackTrace();
    		}
    	}
    	
    	/**
    	 * Loads the mySQL-driver.
    	 * @throws ClassNotFoundException
    	 */
    	public void loadDriver() throws ClassNotFoundException {
    		Class.forName("com.mysql.jdbc.Driver");
    	}
    	
    	/**
    	 * Connects to the mysql-server.
    	 * @throws SQLException
    	 */
    	public void connect() throws SQLException {
    		host = "jdbc:mysql://"+host+"/"+database;
    		this.con = DriverManager.getConnection(host, user, password);
    		this.stmt = this.con.createStatement();
    	}
    	
    	/**
    	 * Reconnects to the mysql-server.
    	 * @throws SQLException
    	 */
    	public void reconnect() throws SQLException {
    		disconnect();
    		connect();
    	}
    	
    	/**
    	 * Disconnects from the mysql-server.
    	 * @throws SQLException
    	 */
    	public void disconnect() throws SQLException {
    		stmt.close();
    		con.close();
    	}
    	
    	/**
    	 * Inserts a message into the database.
    	 * @param pckg The message-package.
    	 * @throws SQLException
    	 */
    	public void insertMessage(Package pckg) throws SQLException {
    		String[] header = pckg.getHeader().split(" ");
    		String from = header[2];
    		String to = header[4];
    		long timestamp = pckg.getDate().getTime();
    		String message = pckg.getContent().firstElement().toString();
    		String query = "INSERT INTO messages ('', '"+from+"', '"+to+"', '"+timestamp+"', '"+message+"');";
    		stmt.executeUpdate(query);
    	}
    	
    	/**
    	 * Returns the host of the mysql-server.
    	 * @return The host of the mysql-server.
    	 */
    	public String getHost() {
    		return host;
    	}
    	
    	/**
    	 * Returns the user of the mysql-server.
    	 * @return The user of the mysql-server.
    	 */
    	public String getUser() {
    		return user;
    	}
    	
    	/**
    	 * Returns the password of the mysql-server.
    	 * @return The password of the mysql-server.
    	 */
    	public String getPassword() {
    		return password;
    	}
    	
    	/**
    	 * Returns the database of the mysql-server.
    	 * @return The database of the mysql-server.
    	 */
    	public String getDatabase() {
    		return database;
    	}
    	
    	/**
    	 * Sets the host.
    	 * @param host The new host.
    	 */
    	public void setHost(String host) {
    		this.host = host;
    	}
    	
    	/**
    	 * Sets the user.
    	 * @param host The new user.
    	 */
    	public void setUser(String user) {
    		this.user = user;
    	}
    	
    	/**
    	 * Sets the password.
    	 * @param host The new password.
    	 */
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	
    	/**
    	 * Sets the database.
    	 * @param host The new database.
    	 */
    	public void setDatabase(String database) {
    		this.database = database;
    	}
    	
    	public static void main(String[] args) throws Exception {
    		MessageTributer tributer = new MessageTributer("localhost","root","c=D1T!5f","chat");
    		Package pckg = new Package();
    		pckg.setHeader("MESSAGE FROM client TO server");
    		pckg.add("Eine Testnachicht.");
    		tributer.insertMessage(pckg);
    		tributer.disconnect();
    	}
    	
    }
  2. Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!

    lima-city: Gratis werbefreier Webspace für deine eigene Homepage

  3. java.lang.NullPointerException at org.silix.chat.server.util.MessageTributer.insertMessage(MessageTributer.java:127)

    bist du dir sicher das in dem String Array header auch was drin steht ?
    also hier

    public void insertMessage(Package pckg) throws SQLException {
            String[] header = pckg.getHeader().split(" ");
            String from = header[2];
            String to = header[4];
    [...]



    Sieht nämlich so aus wie als wäre ein das Array leer bzw. nicht richtig/vollständig gefüllt.


    /edit Hm irgendwie scheint er das Quote hier nicht zu mögen, naja auch egal, ich denk mal es ist klar was ich meine.



    Beitrag geändert: 9.12.2008 19:56:32 von brainworm
  4. Autor dieses Themas

    ork

    ork hat kostenlosen Webspace.

    natürlich steht da was drin
  5. w**s

    Mir scheint das eher ein Problemmit den verwendeten Zeichensätzen zu sein...

    Welchen Zeichensatz erwartet die Datenbank?
  6. Diskutiere mit und stelle Fragen: Jetzt kostenlos anmelden!

    lima-city: Gratis werbefreier Webspace für deine eigene Homepage

Dir gefällt dieses Thema?

Über lima-city

Login zum Webhosting ohne Werbung!