簡體   English   中英

Java RMI 數據庫連接只是停止執行而沒有錯誤消息

[英]Java RMI database connection simply stops executing with no error message

對於作業,我需要實現一個非常基本的 RMI 應用程序,它連接到 MYSQL 數據庫。

我似乎有 RMI 工作,但現在我的 Connection Conn = DriverManager.getConnection 行將無法執行。 沒有錯誤信息。 我嘗試將打印行放入,以查看程序掛起的原因。 它停在那條線上。 我嘗試了不同的 RMI 實現,但是 RMI 工作得很好,但由於某種原因,數據庫不會在這個 RMI 應用程序中連接。 我一定是遺漏了邏輯錯誤或其他東西,請參閱下面的代碼。 有5個班。 StudentDetails、RMIClient、RMIServer、StudentInterface、RMIImpInterface。

謝謝

如果我運行這段代碼,客戶端會:Hello1,Hello2,Hello3 然后停止。 在 hello3 之后,它調用遠程 object。

然后說:你好,在 class.forname 之后

然后停止。 所以由於某種原因,連接不起作用。

數據庫連接與我之前的項目完全相同,運行良好。 我確實也將 jdbc 驅動程序連接到項目(使用 netbeans)

學生詳情 class

package rmiapplication;

import java.io.Serializable;

/**
 *
 * @author Pierre Marais, ZWQQ6GQ12
 */

public class StudentDetails implements Serializable{

    //instance variables
    private String studentNumber;
    private String firstName;
    private String lastName;
    private String contactNumber;
    private String address;

    //getters
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getStudentNumber() {
        return studentNumber;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public String getAddress() {
        return address;
    }

    //constructor
    public StudentDetails(String studentNumber, String firstName, String lastName, String contactNumber, String address) {
        this.studentNumber = studentNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.contactNumber = contactNumber;
        this.address = address;
    }
}

RMIClient class

package rmiapplication;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author Pierre Marais, ZWQQ6GQ12
 */

public class RMIClient extends JFrame {

    static JLabel labStudID;
    static JTextField textStudID;
    static JLabel labFirstName;
    static JTextField textFirstName;
    static JLabel labLastName;
    static JTextField textLastName;
    static JLabel labContNumb;
    static JTextField textContNumb;
    static JLabel labAddress;
    static JTextField textAddress;
    static JButton butClear;
    static JButton butSearch;

    public static void createForm() {
        JFrame fram = new JFrame();

        fram.setTitle("Student details");

        GridLayout layout = new GridLayout();

        labStudID = new JLabel("Student ID");
        textStudID = new JTextField("");
        labFirstName = new JLabel("First Name");
        textFirstName = new JTextField("");
        labLastName = new JLabel("Last Name");
        textLastName = new JTextField("");
        labContNumb = new JLabel("Contact Number");
        textContNumb = new JTextField("");
        labAddress = new JLabel("Address");
        textAddress = new JTextField("");
        butClear = new JButton("Clear");
        butSearch = new JButton("Search");

        fram.add(labStudID);
        fram.add(textStudID);
        fram.add(labFirstName);
        fram.add(textFirstName);
        fram.add(labLastName);
        fram.add(textLastName);
        fram.add(labContNumb);
        fram.add(textContNumb);
        fram.add(labAddress);
        fram.add(textAddress);
        fram.add(butClear);
        fram.add(butSearch);

        fram.setLayout(new GridLayout(6, 2));
        fram.setSize(340, 230);
        fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fram.setVisible(true);
        fram.setLocationRelativeTo(null);

        labStudID.setHorizontalAlignment(JLabel.RIGHT);
        labFirstName.setHorizontalAlignment(JLabel.RIGHT);
        labLastName.setHorizontalAlignment(JLabel.RIGHT);
        labContNumb.setHorizontalAlignment(JLabel.RIGHT);
        labAddress.setHorizontalAlignment(JLabel.RIGHT);

        butClear.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                textStudID.setText("");
                textFirstName.setText("");
                textLastName.setText("");
                textContNumb.setText("");
                textAddress.setText("");
            }
        });

        butSearch.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    System.out.println("hello1");
                    String searchID = textStudID.getText();
                    System.out.println("hello2");
                    StudentInterface remObj = (StudentInterface) Naming.lookup("rmi://127.0.0.1:3306/PIHE2019");
                    System.out.println("hello3");
                    StudentDetails tempStud = remObj.readFromDatabase(searchID);
                    System.out.println("hello4");
                    textStudID.setText(tempStud.getStudentNumber() + "");
                    textFirstName.setText(tempStud.getFirstName());
                    textLastName.setText(tempStud.getLastName());
                    textContNumb.setText(tempStud.getContactNumber());
                    textAddress.setText(tempStud.getAddress());
                } catch (NotBoundException | MalformedURLException | RemoteException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        createForm();
    }
}

學生接口 class

package rmiapplication;

import java.rmi.Remote;
import java.rmi.RemoteException;
/**
 *
 * @author Pierre
 */

public interface StudentInterface extends Remote{

    public StudentDetails readFromDatabase(String studID) throws RemoteException;


}

RMII實現接口class

package rmiapplication;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 *
 * @author Pierre
 */

public class RMIImpInterface extends UnicastRemoteObject implements StudentInterface{

    //Created constructor to throw RemoteException because the super class also throws RemoteException
    public RMIImpInterface() throws RemoteException {
    }

    @Override
    public StudentDetails readFromDatabase(String studID) throws RemoteException {
        StudentDetails student = null;
        try {
            System.out.println("Hi from imp");
            ///register the JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("after class.forname");
            //open connection to database
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/PIHE2019?useSSL=false", "root", "tmz8zhAn7GC6yZvIYSK2E9r1KML7NVNd");
            System.out.println("after connection");
            //here sonoo is database name, root is username and password  
            String query = "SELECT studID FROM details WHERE studID = '"+studID+"'";  
            System.out.println("after query");
            PreparedStatement prepStmt = conn.prepareStatement(query); 
            System.out.println("after prepared statement");
            ResultSet rs = prepStmt.executeQuery(); 
            System.out.println("after after resultset");
            if(rs.next()) { 
                System.out.println("after in if");
                String sID = rs.getString("studID");
                String name = rs.getString("studName");
                String surname = rs.getString("studSurname");
                String contactNumber = rs.getString("studContactNum");
                String address = rs.getString("studAddress"); 

                student = new StudentDetails(sID, name, surname, contactNumber, address);
            }

            rs.close(); 
            prepStmt.close();
        } catch (ClassNotFoundException | SQLException ex) {
            ex.printStackTrace();
        }

        return student;
    }

}

RMI服務器 class

package rmiapplication;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

/**
 *
 * @author Pierre Marais, ZWQQ6GQ12
 */
public class RMIServer {

    public static void main(String[] args) throws RemoteException {
        try {
            Registry remoteRegistry = LocateRegistry.createRegistry(3306);
            remoteRegistry.rebind("PIHE2019", new RMIImpInterface());
            System.out.println("Server is running...");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

我希望數據庫連接,獲取已在GUI中輸入的學號的學生詳細信息,然后將詳細信息填入GUI的rest。 出於某種原因,沒有例外,沒有錯誤消息,什么都沒有。 RMI 似乎可以工作,但由於某種原因,數據庫連接不會(在我以前的項目中工作,沒有錯誤,相同的數據庫代碼)。 謝謝

所以我知道這是一個邏輯錯誤,但很難找到。 不過我確實找到了。 RMIClient 中連接到 RMI 的代碼有效,但它的這種實現導致程序無法與 MySql 一起工作。 正確的代碼是:

Registry reg = LocateRegistry.getRegistry("127.0.0.1", 1099);
StudentInterface remObj = (StudentInterface) reg.lookup("PIHE2019");

代替

StudentInterface remObj = (StudentInterface) Naming.lookup("rmi://127.0.0.1:3306/PIHE2019");

在 RMIClient class 內的搜索按鈕的動作偵聽器中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM