Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库中获取值,根据用户名显示为历史购买记录_Java_Sql_Netbeans - Fatal编程技术网

Java 从数据库中获取值,根据用户名显示为历史购买记录

Java 从数据库中获取值,根据用户名显示为历史购买记录,java,sql,netbeans,Java,Sql,Netbeans,我的系统出了点问题。问题是: 如何根据用户名会话获取表数据。这意味着,当用户登录时,他们可以在其配置文件表单中查看采购项目的历史记录(使用jTable)。提前谢谢 这是我的代码: 1.简介表格 公共类概要文件扩展了javax.swing.JFrame{ String username; DefaultTableModel model; public Profile() { initComponents(); this.setLocationRelativeTo(null); }

我的系统出了点问题。问题是:

如何根据用户名会话获取表数据。这意味着,当用户登录时,他们可以在其配置文件表单中查看采购项目的历史记录(使用jTable)。提前谢谢

这是我的代码:

1.简介表格 公共类概要文件扩展了javax.swing.JFrame{

String username;
DefaultTableModel model;
public Profile() {
    initComponents();
    this.setLocationRelativeTo(null);
}

public Profile(String User) {
    username = User;
    initComponents();
    this.setLocationRelativeTo(null);
}

public void retrieve(){
    DefaultTableModel dth = new Cust_History().getData();

    tblHistory.setModel(dth);
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jTabbedPane1 = new javax.swing.JTabbedPane();
    jPanel1 = new javax.swing.JPanel();
    jPanel2 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    tblHistory = new javax.swing.JTable();
    jButton1 = new javax.swing.JButton();
    jtfUser = new javax.swing.JLabel();
    btnHistory = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Profile Information");
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(java.awt.event.WindowEvent evt) {
            formWindowOpened(evt);
        }
    });

    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 430, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 242, Short.MAX_VALUE)
    );

    jTabbedPane1.addTab("My Profile", jPanel1);

    jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

    tblHistory.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null},
            {null, null, null, null}
        },
        new String [] {
            "Title 1", "Title 2", "Title 3", "Title 4"
        }
    ));
    tblHistory.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            tblHistoryMouseClicked(evt);
        }
    });
    jScrollPane1.setViewportView(tblHistory);

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addGap(55, 55, 55)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(66, Short.MAX_VALUE))
    );

    jTabbedPane1.addTab("Cart History", jPanel2);

    jButton1.setText("Back");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jtfUser.setText("label username");

    btnHistory.setText("Retrieve");
    btnHistory.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnHistoryActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(41, 41, 41)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, `enter code here`javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnHistory))))
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addContainerGap(52, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(37, Short.MAX_VALUE)
            .addComponent(jtfUser, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(12, 12, 12)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)`enter code here`
                .addComponent(btnHistory))
            .addContainerGap())
    );

    pack();
}// </editor-fold>                        

private void tblHistoryMouseClicked(java.awt.event.MouseEvent evt) {                                        
    // TODO add your handling code here:
}                                       

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    jtfUser.setText("Hello "+ username + " have a nice day!");
}                                 
enter code here
private void btnHistoryActionPerformed(java.awt.event.ActionEvent evt) {                                           
   retrieve();
}
}

编辑文章 为什么我不能像这样从配置文件表单中检索用户会话

Profile pf = new Profile(User);
String SQL = "SELECT Artist,SongTitle,Price,Qty FROM history WHERE Username LIKE '"+pf.username+"'";
 PreparedStatement s = con.prepareStatement(SQL);
       ResultSet rs = s.executeQuery(SQL);

不清楚您的问题不清楚哪部分?问题不清楚,您面临什么?我无法从(历史记录表)中获取表数据。这意味着,当用户登录其id时,他可以根据(history table).history数据库表查看以前购买的历史记录*
Profile pf = new Profile(User);
String SQL = "SELECT Artist,SongTitle,Price,Qty FROM history WHERE Username LIKE '"+pf.username+"'";
 PreparedStatement s = con.prepareStatement(SQL);
       ResultSet rs = s.executeQuery(SQL);