【问题标题】:Fetch data from table and display in combo box?从表中获取数据并显示在组合框中?
【发布时间】:2014-04-08 06:25:13
【问题描述】:

Oracle 表::

create table Appointment(App_ID number primary key,
Doctor_ID number,
Patient_ID number,
App_Date Date,
App_Time TIMESTAMP,
App_Charges number);

我有这个作为我的 GUI:2 个按钮“插入”、“显示”

JComboBox combo_time, combo_charges;
JTextField text1, text2, text3, text4;
final String[] sTime = {
        "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", 
        "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", 
        "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45",   
};

final String[] sCharges = {
        "300", "500", "700", "900", "1100", "1300", "1500", 
};

for(int iCtr = 0; iCtr < sTime.length; iCtr++) {
    combo_time.addItem( sTime[iCtr] );
}

for(int iCtr = 0; iCtr < sCharges.length; iCtr++) {
    combo_charges.addItem( sCharges[iCtr] );
}

JDBC::

if(e.getSource()==display) {
    try {
        //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        //Connection      con = DriverManager.getConnection("jdbc:odbc:Employee");
        Class.forName("oracle.jdbc.OracleDriver");
        Connection        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe", "hr", "hr");
        String            t1  = text1.getText();
        int               id  = Integer.parseInt(t1);
        PreparedStatement st  = con.prepareStatement("select * from appointment where App_ID=?");
        st.setInt(1,id);
        ResultSet         rs  = st.executeQuery();

        while(rs.next()) {
            text1.setText(t1);
            text2.setText(Integer.toString(rs.getInt(2)));
            text3.setText(Integer.toString(rs.getInt(3)));
            text4.setText(rs.getString(4));     
            combo_time.addItem(rs.getString("App_Time"));
            combo_charges.addItem(rs.getInt("App_Charges"));
        }
    }

现在我可以从表中获取数据,但我有一列为Date,一列为Timestamp,我不知道如何获取它们并将其转换为字符串,然后在组合框中显示时间(格式hh:mm 但在表格中是 Timestamp),日期在 TextField。 虽然我尝试了上面的代码,但我正在获取数据,但组合框显示的是默认值,即组合框中的第一项。我想要的是在表中显示 App_Time 中的 combo_time 值以对应 @987654331 @ 和 App_Chargescombo_charges 中的值。 请帮忙 谢谢

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    怎么样:

    SimpleDateFormat myFormat = new SimpleDateFormat("HH:mm");
    
    String text1 = myFormat.format(rs.getDate(index1));    
    String text2 = myFormat.format(rs.getTimestamp(index2));
    

    【讨论】:

    • 以及如何为文本字段设置它?像这样 ? t4.setString(text1);对于组合框?
    • 我在我的帖子中编辑了代码,通过它。我正在获取数据,但组合框显示默认值,即组合框中的第一项。我想要的是在 combo_time 表中显示“App_Time”值,以便在 combo_charges 中显示相应的 App_ID 和 App_Charges 值
    • 我做了这个 SimpleDateFormat myFormat = new SimpleDateFormat("dd-mm-yy");字符串 text1 = myFormat.format(rs.getDate(index1)); text4.setText(text1);但是在文本字段中,我得到的日期是“09-00-14”,而在我的表格中是“09-JAN-14”
    • 你读过SimpleDateFormat的javadoc吗?您必须将“HH:mm”传递给构造函数,而不是“dd-mm-yy”。
    • 是的!1 但那是为了时间。我这样做是为了它工作的时间。但我想在文本字段中以 dd-mm-yy 格式显示日期,当我使用“hh:mm”时" ,我的日期文本字段中有时间。
    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    相关资源
    最近更新 更多