Java 选择Query并将显示在Netbeans和mysql中的组合框中

Java 选择Query并将显示在Netbeans和mysql中的组合框中,java,mysql,sql,Java,Mysql,Sql,我在这个代码上遇到了问题: 字符串charitysql=从tbl_charityward、tbl_CharityRom2中选择wardName、CharityRomId,其中tbl_charityward.charityWardID=tbl_CharityRom2.charityWardID 这是我的表格结构: tbl_慈善病房 charityWardID int非空自动增量, 沃德·瓦查尔20, 第20条, 主键charityWardID tbl_Charitiroom2 CHARITYROM

我在这个代码上遇到了问题: 字符串charitysql=从tbl_charityward、tbl_CharityRom2中选择wardName、CharityRomId,其中tbl_charityward.charityWardID=tbl_CharityRom2.charityWardID

这是我的表格结构: tbl_慈善病房 charityWardID int非空自动增量, 沃德·瓦查尔20, 第20条, 主键charityWardID

tbl_Charitiroom2 CHARITYROMID INT NOT NULL自动增量, 第20条, 国际慈善机构, 主键CHARITYROMID、charityWardID、, 外键charityWardID引用tbl_charityward charityWardID

这是我的带有值的表: tbl_慈善病房 +-------+-----+----+ |charityWardID | wardName |状态| +-------+-----+----+ |……1……外科手术……开放| |……2……奥比金……开| |……3……|儿科|……开放| +-------+-----+----+

tbl_Charitiroom2 +-------+----+-------+ |CharityRomid |状态| charityWardID| +-------+----+-------+ |……1…………打开……1| |……2…………打开……1| |……3…………打开……2| +-------+----+-------+

我有两个组合框: cb_ward2=其中包含wardName cb_room2=其中包含CharityRoomID

如果我从cb_ward2中选择一个wardName,那么cb_room2将显示相应的charityRoomID

例如: 我选择外科手术,charityRoomID=1,2将出现在cb_room2上, 当我选择Obygine时,charityRoomID=3只会出现在cb_room2上, 但当我选择儿科时,cb_room2上不会出现charityRoomID

我正在使用Netbeans和MYSQL

编辑:


您使用的方式不是使用PreparedStatement的正确方式

你喜欢这样吗

String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward,tbl_charityroom2 
WHERE tbl_charityward.charityWardID = ?";
pst = conn.prepareStatement(charitysql);
pst.setInt(1,tbl_charityroom2.charityWardID);
rs = pst.executeQuery();
    ArrayList<String> categories = new ArrayList<>();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        System.out.println("\ntrying connection");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/products?user=root&password=");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT DISTINCT categorie FROM product");
        while (rs.next()) {
            categories.add(rs.getString(1));
        }
        for (String item : categories) {
            ComboBoxCategorie.addItem(item);
        }

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) {
            } // ignore
            stmt = null;
        }
    }

由于您的基本要求是将数据显示在两个组合框中,因此可以按以下方式执行:

您可以使用loadcombo来加载带有数据库值的cb_ward2组合框

void loadcombo() {
    try
    {
     Connection conn=null;
 PreparedStatement pst=null;
 ResultSet rs=null;
// Your database connections 

     String charitysql = "SELECT wardName FROM tbl_charityward";
     pst = conn.prepareStatement(charitysql);
     rs = pst.executeQuery();
    while(rs.next()){                            
        cb_ward2.addItem(rs.getString(1));
    }
    con.close();
    }
    catch(Exception e)
    {
        System.out.println("Error"+e);
    }    
}
现在,您可以在cb_ward2上使用ActionListener作为:


尝试使用arrayList!!它会给你一个推力般的效果

像这样的

String charitysql = "SELECT wardName, charityRoomID FROM tbl_charityward,tbl_charityroom2 
WHERE tbl_charityward.charityWardID = ?";
pst = conn.prepareStatement(charitysql);
pst.setInt(1,tbl_charityroom2.charityWardID);
rs = pst.executeQuery();
    ArrayList<String> categories = new ArrayList<>();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        System.out.println("\ntrying connection");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/products?user=root&password=");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT DISTINCT categorie FROM product");
        while (rs.next()) {
            categories.add(rs.getString(1));
        }
        for (String item : categories) {
            ComboBoxCategorie.addItem(item);
        }

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) {
            } // ignore
            stmt = null;
        }
    }

您可以忽略catch和finally子句!!只需关闭stmt

我尝试了您的代码,但出现了错误:“未编译的源代码-错误的树类型:”,我注意到在“pst.setString1,tbl_charityroom2.charityWardID”“tbl_charityroom2”中有红色下划线。我的缺点是将tbl_charityroom而不是tbl_charityroom2。您的表名是tbl_charityroom还是tbl_charityroom2?pst.SetString 1,tbl_charityroom2.charityWardID应该是pst.setInt1,tbl_charityroom2.charityWardID因为charityWardID为int,请查看编辑没有发生任何事情。与pst.setString相同,有红色下划线声明它是什么意思?对不起,我是新来的netbeans@Jugadu谢谢你纠正这个错误
    ArrayList<String> categories = new ArrayList<>();
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        System.out.println("\ntrying connection");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/products?user=root&password=");
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT DISTINCT categorie FROM product");
        while (rs.next()) {
            categories.add(rs.getString(1));
        }
        for (String item : categories) {
            ComboBoxCategorie.addItem(item);
        }

    } catch (SQLException ex) {
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException sqlEx) {
            } // ignore
            stmt = null;
        }
    }