DB Verbindung zu MySQL



  • Hallo zusammen

    In meinem Programm möchte ich was aus einer MySQL DB lesen. Doch bei der instanzierung des Treibers geht was schief. Kann mir jemand sagen was ich falsch mache?

    //Zugriff auf Datenbank erstellen
                Class.forName("java.sql.Driver").newInstance();
                // Diese Eintraege werden zum 
                // Verbindungsaufbau benoetigt. 
                String hostname = "localhost"; 
                String port = "3306"; 
                String dbname = "Adresse"; 
                String user = "root"; 
                String password = "aa_root";
    
    //            Connection conn = null; 
                String url = "jdbc:mysql://"+hostname+":"+port+"/"+dbname; 
                Connection conn = java.sql.DriverManager.getConnection(url, user, password);
    
    //	    conn = DriverManager.getConnection(url, user, password);
    
                conn.close();
    

    Wenn ich oben stehenden code ausführe, kommt immer folgende Fehlermeldung.

    SCHWERWIEGEND: null
    java.lang.InstantiationException: java.sql.Driver
    	at java.lang.Class.newInstance(Class.java:427)
    	at TestSourc.TestProgramm.mnu_test_dlgActionPerformed(TestProgramm.java:205)
    	at TestSourc.TestProgramm.access$200(TestProgramm.java:24)
    	at TestSourc.TestProgramm$3.actionPerformed(TestProgramm.java:131)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    	at java.awt.Component.processMouseEvent(Component.java:6533)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    	at java.awt.Component.processEvent(Component.java:6298)
    	at java.awt.Container.processEvent(Container.java:2236)
    	at java.awt.Component.dispatchEventImpl(Component.java:4889)
    	at java.awt.Container.dispatchEventImpl(Container.java:2294)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    	at java.awt.Container.dispatchEventImpl(Container.java:2280)
    	at java.awt.Window.dispatchEventImpl(Window.java:2746)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    	at java.awt.EventQueue$4.run(EventQueue.java:731)
    	at java.awt.EventQueue$4.run(EventQueue.java:729)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Caused by: java.lang.NoSuchMethodException: java.sql.Driver.<init>()
    	at java.lang.Class.getConstructor0(Class.java:3082)
    	at java.lang.Class.newInstance(Class.java:412)
    	... 42 more
    

    Was muss ich ändern damit ich eine Verbindung hinkriege?

    Danke für jede Hilfe.

    Gruss Renato



  • Für MySql benötigst du auch den passenden Treibernamen, s. Treiber laden oder Connecting to MySQL Using the JDBC DriverManager Interface:

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

    "java.sql.Driver" ist nur der Name der Schnittstelle (interface).



  • Ciao Th69

    Gut nun kommt aber folgende Fehlermeldung.

    SCHWERWIEGEND: null
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    	at java.lang.Class.forName0(Native Method)
    	at java.lang.Class.forName(Class.java:264)
    	at TestSourc.TestProgramm.mnu_test_dlgActionPerformed(TestProgramm.java:205)
    	at TestSourc.TestProgramm.access$200(TestProgramm.java:24)
    	at TestSourc.TestProgramm$3.actionPerformed(TestProgramm.java:131)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    	at java.awt.Component.processMouseEvent(Component.java:6533)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    	at java.awt.Component.processEvent(Component.java:6298)
    	at java.awt.Container.processEvent(Container.java:2236)
    	at java.awt.Component.dispatchEventImpl(Component.java:4889)
    	at java.awt.Container.dispatchEventImpl(Container.java:2294)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    	at java.awt.Container.dispatchEventImpl(Container.java:2280)
    	at java.awt.Window.dispatchEventImpl(Window.java:2746)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    	at java.awt.EventQueue$4.run(EventQueue.java:731)
    	at java.awt.EventQueue$4.run(EventQueue.java:729)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    

    Da habe ich wohl bei der Installation was falsch gemacht. Was ich noch nachtragen sollte ist, dass ich mit Ubuntu 17.04 arbeite und eine MySQL 5.7 installiert habe.

    Gruss Renato



  • Hast du mal "DriverManager.getDrivers()" (aus dem ersten Link) bei dir aufgerufen?



  • Ciao Th69

    Das funktioniert auch nicht. Denn das ganze Programm bleibt bereits bei der Zeile

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

    hängen.
    Wahrscheinlich habe ich bei der Installation was falsch gemacht.

    Gruss Renato



  • Du sollst natürlich vorher (bzw. stattdessen) diese Methode aufrufen!

    Als Software-Entwickler mußt du schon mal ein bißchen nachdenken 😉



  • Keine Change! Nun wird das Programm mit dem folgenden Fehler quitiert

    Apr 24, 2017 9:57:11 AM TestSourc.TestProgramm mnu_test_dlgActionPerformed
    SCHWERWIEGEND: null
    java.sql.SQLException: No suitable driver
    	at java.sql.DriverManager.getDriver(DriverManager.java:315)
    	at TestSourc.TestProgramm.mnu_test_dlgActionPerformed(TestProgramm.java:207)
    	at TestSourc.TestProgramm.access$200(TestProgramm.java:26)
    	at TestSourc.TestProgramm$3.actionPerformed(TestProgramm.java:133)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    	at java.awt.Component.processMouseEvent(Component.java:6533)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    	at java.awt.Component.processEvent(Component.java:6298)
    	at java.awt.Container.processEvent(Container.java:2236)
    	at java.awt.Component.dispatchEventImpl(Component.java:4889)
    	at java.awt.Container.dispatchEventImpl(Container.java:2294)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    	at java.awt.Container.dispatchEventImpl(Container.java:2280)
    	at java.awt.Window.dispatchEventImpl(Window.java:2746)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    	at java.awt.EventQueue$4.run(EventQueue.java:731)
    	at java.awt.EventQueue$4.run(EventQueue.java:729)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at org.GNOME.Accessibility.AtkWrapper$5.dispatchEvent(AtkWrapper.java:700)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    

    Gruss Renato



  • Hallo zusammen

    Ich konnte nun das Problem lösen. Man muss die jar-Datei im Projekt einbinden. Dann geht's.

    Danke an allen die mir weiter geholfen haben.

    Gruss Renato


Anmelden zum Antworten