从数据库搜索并显示输出 [英] searching from database and showing the output

查看:129
本文介绍了从数据库搜索并显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JTable,我要在其中显示数据库表中的所有数据,我有一个JTextField,我要在其中搜索字符串,例如联系人的"FIRSTNAME",然后我想使用JOptionPane在消息中显示输出.

I have a JTable where I am displaying all the data from my database table, and I have a JTextField where I am going to search the string for example the "FIRSTNAME" of the contact and then I want to display the output in a message using JOptionPane.

请告诉我我做错了.

package ContactPackage;

import java.sql.Connection;
import javax.swing.JOptionPane;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;

public class viewContacts extends javax.swing.JFrame {

Connection conn = null;
OraclePreparedStatement pst = null;
OracleResultSet rs = null;

public viewContacts() {
    initComponents();
}

private void searchContacts()
{
    conn = JavaConnectDb.ConnecterDb();
    try
    {
        String sql = "select * from CONTACTS where FNAME=?";
        pst = (OraclePreparedStatement) conn.prepareStatement(sql);
        pst.setString(1, searchContact.getText());
        rs = (OracleResultSet) pst.executeQuery();
        if(rs.next()) {
            JOptionPane.showMessageDialog(null, sql);

        }else{
            JOptionPane.showMessageDialog(null, "Not Found");
        }

    }catch (Exception e) { JOptionPane.showMessageDialog(null, e); }
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    searchContacts();
} 

推荐答案

    if(rs.next()) {
        JOptionPane.showMessageDialog(null, sql);

您将在此处显示SQL查询文本,而不是其结果.
您需要从返回的结果集rs中获取结果.

You are displaying the SQL query text here, not the results from it.
You need to get the results from the returned result set rs.

这篇关于从数据库搜索并显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆