• Es freut uns dass du in unser Minecraft Forum gefunden hast. Hier kannst du mit über 130.000 Minecraft Fans über Minecraft diskutieren, Fragen stellen und anderen helfen. In diesem Minecraft Forum kannst du auch nach Teammitgliedern, Administratoren, Moderatoren , Supporter oder Sponsoren suchen. Gerne kannst du im Offtopic Bereich unseres Minecraft Forums auch über nicht Minecraft spezifische Themen reden. Wir hoffen dir gefällt es in unserem Minecraft Forum!

SQL Connection

Payno4

Schafhirte
Registriert
12 Februar 2015
Beiträge
127
Diamanten
300
Hei Community ,
leider funktioniert meine Connection zu meinem SQL Server nicht , er sagt mir immer entweder :" Unknow database ... " oder ,"com.mysql.jdbc.JDBC4Connection cannot be cast to main.Connection". Doch beide Database gibt es.

Ich hoffe ihr könnt mir helfen. :)
Code:
publicConnectioncon(){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://localhost:3306/newSch?user=root&password=password&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

          

            returnconnection;

        

        }

        catch(Exceptionex){

            System.out.println("A problem has appered with the Mysql.");

            System.out.println(ex.getMessage());

            returnnull;

        }

    }

Klasse :
Code:
package main;



import java.sql.DriverManager;



publicclassConnection{

   

    Connectionconnection;

   

    publicConnection(){

         

    }

   

    publicbooleanconnectToMysql(Stringhost,Stringdatabase,Stringuser,Stringpasswd){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://"+host+"/"+database+"?user="+user+"&password="+passwd+"&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

            returntrue;

         

        }

        catch(Exceptionex){

            System.out.println(ex.getMessage());

            returnfalse;

        }

    }

   

    /*

    * Don't forget to change the connectionCommand if there is a modifaktion.

    * Check connectToMysql to compare.

    */

    publicConnectioncon(){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://localhost:3306/newSch?user=root&password=password&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

           

            returnconnection;

         

        }

        catch(Exceptionex){

            System.out.println("A problem has appered with the Mysql.");

            System.out.println(ex.getMessage());

            returnnull;

        }

    }

}

Java Version : 1.8
Server: MySQL Community Server (GPL)
Version: 5.7.15
 
Zuletzt bearbeitet:

Kroseida

Schafhirte
Registriert
28 September 2015
Beiträge
110
Alter
28
Diamanten
307
Minecraft
Kroseida
Hei Community ,
leider funktioniert meine Connection zu meinem SQL Server nicht , er sagt mir immer entweder :" Unknow database ... " oder ,"com.mysql.jdbc.JDBC4Connection cannot be cast to main.Connection". Doch beide Database gibt es.

Ich hoffe ihr könnt mir helfen. :)
Code:
publicConnectioncon(){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://localhost:3306/newSch?user=root&password=password&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

        

            returnconnection;

      

        }

        catch(Exceptionex){

            System.out.println("A problem has appered with the Mysql.");

            System.out.println(ex.getMessage());

            returnnull;

        }

    }

Klasse :
Code:
package main;



import java.sql.DriverManager;



publicclassConnection{

 

    Connectionconnection;

 

    publicConnection(){

       

    }

 

    publicbooleanconnectToMysql(Stringhost,Stringdatabase,Stringuser,Stringpasswd){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://"+host+"/"+database+"?user="+user+"&password="+passwd+"&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

            returntrue;

       

        }

        catch(Exceptionex){

            System.out.println(ex.getMessage());

            returnfalse;

        }

    }

 

    /*

    * Don't forget to change the connectionCommand if there is a modifaktion.

    * Check connectToMysql to compare.

    */

    publicConnectioncon(){

        try{

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            StringconnectionCommand="jdbc:mysql://localhost:3306/newSch?user=root&password=password&useSSL=false";

            connection=(Connection)DriverManager.getConnection(connectionCommand);

         

            returnconnection;

       

        }

        catch(Exceptionex){

            System.out.println("A problem has appered with the Mysql.");

            System.out.println(ex.getMessage());

            returnnull;

        }

    }

}

Java Version : 1.8
Server: MySQL Community Server (GPL)
Version: 5.7.15
Du solltest deine Klasse umbennen, "Connection" ist schon vergeben.
Und caste es auch nicht auf Connection.
 

TheSimufreak

Kuhfänger
Registriert
28 Juni 2012
Beiträge
78
Diamanten
0
Du solltest deine Klasse umbennen, "Connection" ist schon vergeben.Und caste es auch nicht auf Connection
Vollkommen richtig.
Connection connection;
[...]
connection=(Connection)DriverManager.getConnection(connectionCommand);
Wenn du eine Klasse angibst sucht Java zuerst in dem aktuellen Package, findet sich dort die gesuchte Klasse nicht, sucht Java in den Imports.
Entsprechend ist deine 'Connection' nicht von dem Typ, mit dem der 'DriverManager' arbeitet.
 
Oben