第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用線程發(fā)出數(shù)據(jù)庫請(qǐng)求

使用線程發(fā)出數(shù)據(jù)庫請(qǐng)求

紅糖糍粑 2019-06-18 15:30:46
使用線程發(fā)出數(shù)據(jù)庫請(qǐng)求我試圖了解線程在java中是如何工作的。這是一個(gè)返回ResultSet的簡單數(shù)據(jù)庫請(qǐng)求。我在使用JavaFX。    package application;import java.sql.ResultSet;import java.sql.SQLException;import javafx.fxml.FXML;import javafx.scene.control.Button;     import javafx.scene.control.Label;import javafx.scene.control.TextField;public class Controller{     @FXML     private Button getCourseBtn;     @FXML     private TextField courseId;     @FXML     private Label courseCodeLbl;     private ModelController mController;     private void requestCourseName(){         String courseName = "";         Course c = new Course();         c.setCCode(Integer.valueOf(courseId.getText()));         mController = new ModelController(c);         try {             ResultSet rs = mController.<Course>get();             if(rs.next()){                 courseCodeLbl.setText(rs.getString(1));             }         } catch (SQLException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }//      return courseName;     }     public void getCourseNameOnClick(){         try {//              courseCodeLbl.setText(requestCourseName());             Thread t = new Thread(new Runnable(){                 public void run(){                     requestCourseName();                 }             }, "Thread A");             t.start();         } catch (NumberFormatException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }     }}這將返回一個(gè)異常:線程“Thread A”中的異常java.lang.IllegalStateException:不在FX應(yīng)用程序線程上;currentThread=Thread A如何正確地實(shí)現(xiàn)線程處理,以便在第二個(gè)線程而不是主線程中執(zhí)行每個(gè)數(shù)據(jù)庫請(qǐng)求?我聽說過實(shí)現(xiàn)Runnable,但是如何在Run方法中調(diào)用不同的方法呢?以前從來沒有用過線程,但我想是時(shí)候開始了。
查看完整描述

3 回答

?
慕尼黑的夜晚無繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

線程“Thread A”中的異常java.lang.IllegalStateException:不在FX應(yīng)用程序線程上;currentThread=Thread A

例外情況是試圖告訴您,您正在嘗試訪問JavaFX應(yīng)用程序線程之外的JavaFX場景圖。但在哪里?

courseCodeLbl.setText(rs.getString(1)); // <--- The culprit

如果我不能這樣做,我如何使用背景線程?

這是不同的方法,導(dǎo)致類似的解決方案。

用Platform.runLater包裝場景圖元素

有一種更簡單、最簡單的方法是將上面的行封裝在Plaform.runLater,以便在JavaFX應(yīng)用程序線程上執(zhí)行。

Platform.runLater(() -> courseCodeLbl.setText(rs.getString(1)));

使用任務(wù)

這個(gè)更好的方法與這些場景一起使用的方法是任務(wù),它具有發(fā)送更新的專門方法。在下面的示例中,我將使用updateMessage更新消息。此屬性綁定到courseCodeLblTextProperty

Task<Void> task = new Task<Void>() {
    @Override
    public Void call() {
        String courseName = "";
        Course c = new Course();
        c.setCCode(Integer.valueOf(courseId.getText()));
        mController = new ModelController(c);
        try {
            ResultSet rs = mController.<Course>get();
            if(rs.next()) {
                // update message property
                updateMessage(rs.getString(1));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }}public void getCourseNameOnClick(){
    try {
        Thread t = new Thread(task);
        // To update the label
        courseCodeLbl.textProperty.bind(task.messageProperty());
        t.setDaemon(true); // Imp! missing in your code
        t.start();
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}


查看完整回答
反對(duì) 回復(fù) 2019-06-18
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

這與數(shù)據(jù)庫無關(guān)。JavaFX和幾乎所有GUI庫一樣,要求您只使用主UI線程來修改GUI。

您需要將數(shù)據(jù)從數(shù)據(jù)庫傳遞回主UI線程。使用Platform.runLater()安排在主UI線程中運(yùn)行的Runnable。

public void getCourseNameOnClick(){
    new Thread(new Runnable(){
        public void run(){
            String courseName = requestCourseName();
            Platform.runLater(new Runnable(){
                courseCodeLbl.setText(courseName)
            });
        }
    }, "Thread A").start();}

或者,你可以使用任務(wù).


查看完整回答
反對(duì) 回復(fù) 2019-06-18
  • 3 回答
  • 0 關(guān)注
  • 550 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)