2012-06-03 30 views
0

enter image description here在下面的代碼中..我得到沒有合適的驅動程序..錯誤: 請幫助。我已經使用了它一個小時了,而且似乎無法解決它。mysql java沒有合適的驅動程序

這很瘋狂。在我開始搞亂GWT之前,我能夠更早地做到這一點。

package com.gwt.churchweb.churchweblogin.client; 

import com.google.gwt.user.client.ui.Composite; 

import com.google.gwt.user.client.ui.VerticalPanel; 
import com.google.gwt.user.client.ui.Label; 
import com.google.gwt.user.client.ui.FlexTable; 
import com.google.gwt.user.client.ui.TextBox; 
import com.google.gwt.user.client.ui.HasHorizontalAlignment; 
import com.google.gwt.user.client.ui.CheckBox; 
import com.google.gwt.user.client.ui.Button; 
import com.google.gwt.event.dom.client.ClickHandler; 
import com.google.gwt.event.dom.client.ClickEvent; 
import com.google.gwt.user.client.Window; 
import java.sql.Connection; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.sql.DriverManager; 

import java.util.logging.Level; 
import java.util.logging.Logger; 

public class Login extends Composite { 

    public Login() { 

     VerticalPanel verticalPanel = new VerticalPanel(); 
     initWidget(verticalPanel); 
     verticalPanel.setSize("329px", "186px"); 

     Label lblNewLabel = new Label("Sign into your account"); 
     lblNewLabel.setStyleName("gwt-Login-SigninLabel"); 
     verticalPanel.add(lblNewLabel); 

     FlexTable flexTable = new FlexTable(); 
     verticalPanel.add(flexTable); 
     flexTable.setWidth("308px"); 

     Label lblNewLabel_1 = new Label("Username:"); 
     lblNewLabel_1.setStyleName("gwt-Label-Login"); 
     flexTable.setWidget(0, 0, lblNewLabel_1); 
     lblNewLabel_1.setWidth("72px"); 

     final TextBox textboxUsername = new TextBox(); 
     textboxUsername.setStyleName("gwt-LoginTextBox"); 
     flexTable.setWidget(0, 1, textboxUsername); 
     textboxUsername.setWidth("204px"); 

     Label lblNewLabel_2 = new Label("Password:"); 
     lblNewLabel_2.setStyleName("gwt-Label-Login"); 
     flexTable.setWidget(1, 0, lblNewLabel_2); 
     lblNewLabel_2.setWidth("66px"); 

     final TextBox textBoxPassword = new TextBox(); 
     textBoxPassword.setStyleName("gwt-LoginTextBox"); 
     flexTable.setWidget(1, 1, textBoxPassword); 
     textBoxPassword.setWidth("204px"); 
     flexTable.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT); 
     flexTable.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT); 

     CheckBox chckbxRememberMeOn = new CheckBox("Remember me on this computer"); 
     chckbxRememberMeOn.setStyleName("gwt-Checkbox-Login"); 
     flexTable.setWidget(2, 1, chckbxRememberMeOn); 

     Button btnSignIn = new Button("Sign In"); 
     btnSignIn.addClickHandler(new ClickHandler() { 
      public void onClick(ClickEvent event) { 
       Connection con = null; 
       Statement st = null; 
       ResultSet rs = null; 

       String url = "jdbc:mysql://localhost:3306/churchweb"; 
       String user = "root"; 
       String password = "*****"; 

       try { 
        con = DriverManager.getConnection(url, user, password); 
        st = con.createStatement(); 
        rs = st.executeQuery("SELECT VERSION()"); 
Window.alert("Fixing to try it"); 
        if (rs.next()) { 
         Window.alert(rs.getString(1)); 
        } 

       } catch (SQLException ex) { 
        Logger lgr = Logger.getLogger(Login.class.getName()); 
        lgr.log(Level.SEVERE, ex.getMessage(), ex); 

       } finally { 
        try { 
         if (rs != null) { 
          rs.close(); 
         } 
         if (st != null) { 
          st.close(); 
         } 
         if (con != null) { 
          con.close(); 
         } 

        } catch (SQLException ex) { 
         Logger lgr = Logger.getLogger(Login.class.getName()); 
         lgr.log(Level.WARNING, ex.getMessage(), ex); 
        } 
       } 





       if (textboxUsername.getText().length() == 0 
         || textBoxPassword.getText().length() == 0) { 
         Window.alert("Username or password is empty."); 
        } 
      } 
     }); 

     btnSignIn.setStyleName("gwt-Login-SigninButton"); 
     flexTable.setWidget(3, 1, btnSignIn); 

    } 


} 
+0

我相信已經提供了兩個答案來解決這個問題。你在哪裏加載驅動程序?我在代碼發佈中沒有看到這一點。這必須在您的代碼中完成,以便jvm加載「合適的」驅動程序。只有.jar不會解決您的問題。 – Freddy

+0

String url =「jdbc:mysql:// localhost:3306/churchweb」; String user =「root」;弗雷迪,<==不會加載驅動程序? –

回答

2

確保MySQL Connector/J驅動程序jar文件位於您的類路徑中。

+0

我該怎麼做? –

+0

從鏈接下載文件。提取它,你會在lib文件夾中找到一個.jar文件。將該文件複製到項目的lib /文件夾中。 – Jeshurun

+0

哦,我做到了...一秒生病發布截圖,如果我允許 –

2

您尚未將驅動程序加載到您的應用程序,請嘗試添加Class.forName(「com.mysql.jdbc.Driver」);

1

您需要加載驅動程序。這個文檔中有幾個例子與sun的網頁中的connector-j驅動程序一起提供。以下是該文檔的一部分。

try { 
    Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    } catch (Exception ex) { 
    // handle the error 
    } 

如果您想了解更多有關加載Java的JDBC驅動程序看看那被你的驅動下載捆綁在一起的連接器j.pdf的第6章。

相關問題