我正在使用Netbeans上的Java中的字典項目。我這裏有兩類:無法從Netbeans中的Mysql檢索數據
「dictionary.java」裏的主要方法是
「DictionaryGuiController.java」,其中GUI代碼構造與JavaFX平臺
我連接數據庫和項目與JDBC驅動程序和使用這些代碼在主方法:
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/world", "root", "root");
statement = conn.createStatement();
rs = statement.executeQuery("SELECT * FROM country");
while (rs.next()) {
System.out.println(rs.getString("code") + ":" + rs.getString("name"));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
在這裏,我創建的連接對象作爲conn
創建語句和執行的SQL查詢。 我想從Mysql中的示例數據庫檢索名爲「world」的數據。通過這段代碼,我可以在只有一個類和主要方法的小型項目中檢索數據。但是,在這個項目中,當我運行程序我看到了GUI界面,但我看不到任何結果在控制檯,它口口聲聲說:
執行C:\用戶\酒吧\文檔\的NetBeansProjects \字典\ DIST \ run414351490 \ Dictionary.jar使用平臺C:\ Program Files \ Java \ jdk1.7.0_45 \ jre/bin/java
程序不會停止,直到程序退出。
下面是類的完整代碼:
Dictionary.java
:
package dictionary;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.sql.*;
public class Dictionary extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("DictionaryGui.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/world", "root", "root");
statement = conn.createStatement();
rs = statement.executeQuery("SELECT * FROM country");
while (rs.next()) {
System.out.println(rs.getString("code") + ":" + rs.getString("name"));
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
}
}
DictionaryGuiController.java
:
package dictionary;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class DictionaryGuiController implements Initializable {
@FXML
private TextField searchfield;
@FXML
private Button buttonsearch;
@FXML
private TextArea listview;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void handleButtonAction(ActionEvent event) {
listview.setText(searchfield.getText());
}
}
可能是什麼問題呢?任何幫助,將不勝感激。
你是否從數據庫獲取數據或不... –
我想你忘了註冊驅動程序的MySQL。在連接到數據庫之前,請嘗試使用** Class.forName(「com.mysql.jdbc.Driver」)**。 –