我正在使用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”)**。 –