Mysql und Java Fehler :-(

gamerfunkie

Erfahrenes Mitglied
Hallo,
ich habe vor java mit meiner lokal laufenden Mysql Datwenbank zu verbinden. Ich bekomme aber immer den Fehler:

Code:
java.sql.SQLException: No suitable driver
Name des Datenbanktreibers: com.mysql.jdbc.Driver
Url der Datenbank: jdbc:mysql://localhost:3606
Name der Tabelle eingeben (z.B. MeineTestTabelle): mw_data
Benutzername: root
Passwort:
java.sql.SQLException: No suitable driver

Mein Code sieht so aus:
Code:
// DbTableShow.java

import java.sql.*;
import java.io.*;

public class DbTableShow
{
  public static void main( String[] argv )
  {
    String sDbDrv=null, sDbUrl=null, sTable=null, sUsr="", sPwd="";
    if( 3 <= argv.length ) {
      sDbDrv = argv[0];
      sDbUrl = argv[1];
      sTable = argv[2];
      if( 4 <= argv.length )  sUsr = argv[3];
      if( 5 <= argv.length )  sPwd = argv[4];
    } else {
       {       
        sDbDrv = "com.mysql.jdbc.Driver"; 
        sDbUrl = "jdbc:mysql://localhost:3606";     
        sUsr = "root";
        sPwd = "";
        sTable = "mw_data";    
        
        try
        {
            System.out.println(DriverManager.getConnection(sDbUrl));
        }
        catch(Exception ex ) 
        {
            System.out.println( ex );
          }
        
        System.out.println( "Name des Datenbanktreibers: " + sDbDrv );
        System.out.println( "Url der Datenbank: " + sDbUrl);
        System.out.println( "Name der Tabelle eingeben (z.B. MeineTestTabelle): " + sTable);
        System.out.println( "Benutzername: " + sUsr);                    
        System.out.println( "Passwort: " + sPwd);
      }
    }
    if( null != sDbDrv && 0 < sDbDrv.length() &&
        null != sDbUrl && 0 < sDbUrl.length() &&
        null != sTable && 0 < sTable.length() ) {
      Connection cn = null;
      Statement  st = null;
      ResultSet  rs = null;
      try {
        // Select fitting database driver and connect:
        Class.forName( sDbDrv );
        cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
        st = cn.createStatement();
        rs = st.executeQuery( "select * from " + sTable );
        // Get meta data:
        ResultSetMetaData rsmd = rs.getMetaData();
        int i, n = rsmd.getColumnCount();
        // Print table content:
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
        for( i=1; i<=n; i++ )    // Attention: first column with 1 instead of 0
          System.out.print( "| " + extendStringTo14( rsmd.getColumnName( i ) ) );
        System.out.println( "|" );
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
        while( rs.next() ) {
          for( i=1; i<=n; i++ )  // Attention: first column with 1 instead of 0
            System.out.print( "| " + extendStringTo14( rs.getString( i ) ) );
          System.out.println( "|" );
        }
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
      } catch( Exception ex ) {
        System.out.println( ex );
      } finally {
        try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
        try { if( null != st ) st.close(); } catch( Exception ex ) {}
        try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
      }
    }
  }

  // Extend String to length of 14 characters
  private static final String extendStringTo14( String s )
  {
    if( null == s ) s = "";
    final String sFillStrWithWantLen = "              ";
    final int iWantLen = sFillStrWithWantLen.length();
    final int iActLen  = s.length();
    if( iActLen < iWantLen )
      return (s + sFillStrWithWantLen).substring( 0, iWantLen );
    if( iActLen > 2 * iWantLen )
      return s.substring( 0, 2 * iWantLen );
    return s;
  }
}

meine Datenbank läuft auf Port 3306
ich habe installiert: Java EE SDK 5
Den Mysql-Connector in der Version 5 habe ich mir geladen und in das Verzeichnis ...\Java\jre\lib\ext gepackt ...
Meine CLASSPATH-variable sieht so aus:
Code:
.;C:\Programme\Java\jre1.5.0_06\lib\ext\mysql-connector-java-5.0.4-bin.jar

Was mache ich falsch?
Mfg gamerfunkie
 
Der Treiber wird nicht geladen: sDbDrv wird nirgends verwendet.

Eine Zeile a la
Java:
Class.forName(sDbDrv).newInstance();
fehlt.

Und sowieso gehört die Herstellung der Datenbankverbindung nicht in den else-Zweig, oder?
 
Zuletzt bearbeitet:
Danke für die schnelle Antwort. Ich habs jetzt geändert, aber der Fehler ist trotzdem noch da. Ich hab auch irgendwo gelesen, das man das mit NewInstance nicht brauch. Das
Code:
 Class.Forname(..
hatte ich ja schon drin
Mfg gamerfunkie
 
Wie meinstr du das jetzt im else-Zweig. Tut mir Leid wenn ich mich komisch anstelle, aber es ist ja auch nicht mein Code ^^. Ich will nur sehen ob alles läuft usw.
 
Ich habe jetzt den Code geändert .. Man muss statt jdbc:mysql:http://loca...
jdbc:mysql://localhost ... schreiben(http: weglassen irgfendwie auch logisch ..)

Jetzt hab ich ne neue Fehlermeldung:
Code:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.SocketException: Unrecognized Windows Sockets error: 10106: create

STACKTRACE:

java.net.SocketException: java.net.SocketException: Unrecognized Windows Sockets error: 10106: create
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
        at java.sql.DriverManager.getConnection(DriverManager.java:525)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at DbTableShow.main(DbTableShow.java:28)


** END NESTED EXCEPTION **



Last packet sent to the server was 31 ms ago.

C:\XAMPP\xampp\htdocs\MEDIEVAL WARS\TESTS\karte_java>

Wenn ich statt 127.0.0.1 localhost schreibe kommt folgende Fehlermeldun:

Code:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.UnknownHostException
MESSAGE: localhost: localhost

STACKTRACE:

java.net.UnknownHostException: localhost: localhost
        at java.net.InetAddress.getAllByName0(InetAddress.java:1128)
        at java.net.InetAddress.getAllByName0(InetAddress.java:1098)
        at java.net.InetAddress.getAllByName(InetAddress.java:1061)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:137)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
        at com.mysql.jdbc.Connection.<init>(Connection.java:1531)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
        at java.sql.DriverManager.getConnection(DriverManager.java:525)
        at java.sql.DriverManager.getConnection(DriverManager.java:171)
        at DbTableShow.main(DbTableShow.java:28)


** END NESTED EXCEPTION **



Last packet sent to the server was 15 ms ago.

C:\XAMPP\xampp\htdocs\MEDIEVAL WARS\TESTS\karte_java>



Hier noch der neue Code:
Code:
// DbTableShow.java

import java.sql.*;
import java.io.*;

public class DbTableShow
{
  public static void main( String[] argv )
  {
    String sDbDrv=null, sDbUrl=null, sTable=null, sUsr="", sPwd="";
          
    sDbDrv = "com.mysql.jdbc.Driver"; 
    sDbUrl = "jdbc:mysql://127.0.0.1:3306/db_data";     
    sUsr = "root";
    sPwd = "";
    sTable = "user";    
    if( null != sDbDrv && 0 < sDbDrv.length() &&
        null != sDbUrl && 0 < sDbUrl.length() &&
        null != sTable && 0 < sTable.length() ) 
    {    
      Connection cn = null;
      Statement  st = null;
      ResultSet  rs = null;
      try 
      {
        // Select fitting database driver and connect:
        Class.forName(sDbDrv).newInstance(); 
        cn = DriverManager.getConnection( sDbUrl, sUsr, sPwd );
        st = cn.createStatement();
        rs = st.executeQuery( "select * from " + sTable );
        // Get meta data:
        ResultSetMetaData rsmd = rs.getMetaData();
        int i, n = rsmd.getColumnCount();
        // Print table content:
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
        for( i=1; i<=n; i++ )    // Attention: first column with 1 instead of 0
          System.out.print( "| " + extendStringTo14( rsmd.getColumnName( i ) ) );
        System.out.println( "|" );
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
        while( rs.next() ) {
          for( i=1; i<=n; i++ )  // Attention: first column with 1 instead of 0
            System.out.print( "| " + extendStringTo14( rs.getString( i ) ) );
          System.out.println( "|" );
        }
        for( i=0; i<n; i++ )
          System.out.print( "+---------------" );
        System.out.println( "+" );
          } catch( Exception ex ) {
        System.out.println( ex );
      } finally {
        try { if( null != rs ) rs.close(); } catch( Exception ex ) {}
        try { if( null != st ) st.close(); } catch( Exception ex ) {}
        try { if( null != cn ) cn.close(); } catch( Exception ex ) {}
      }
    }
  }

  // Extend String to length of 14 characters
  private static final String extendStringTo14( String s )
  {
    if( null == s ) s = "";
    final String sFillStrWithWantLen = "              ";
    final int iWantLen = sFillStrWithWantLen.length();
    final int iActLen  = s.length();
    if( iActLen < iWantLen )
      return (s + sFillStrWithWantLen).substring( 0, iWantLen );
    if( iActLen > 2 * iWantLen )
      return s.substring( 0, 2 * iWantLen );
    return s;
  }
}
 
Zuletzt bearbeitet:
Zurück