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

时间:2015-12-07 13:25:09

标签: java sql netbeans

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

如何根据用户名会话获取表数据。意味着,当用户登录时,他们可以在他们的个人资料表格中查看购买项目的历史记录(使用jTable)。提前谢谢。

这是我的代码:

1.Profile Form

public class Profile扩展了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();
}

2。 Cust_History类

公共类Cust_History {

String User;

String conString = "jdbc:mysql://localhost:3306/softwaredesign";
String username = "root";
String password = "";

//CREATE TABLE
public DefaultTableModel getData(){
DefaultTableModel dth = new DefaultTableModel();
dth.addColumn("Artist");
dth.addColumn("SongTitle");
dth.addColumn("Price");
dth.addColumn("Qty");



    try {
       Connection con = DriverManager.getConnection(conString, username, password);
       Profile pf = new Profile(User);

           //SQL STATEMENT
       String SQL = "SELECT Artist,SongTitle,Price,Qty FROM history WHERE Username LIKE '"+User+"'";

       //PST
       PreparedStatement s = con.prepareStatement(SQL);
       ResultSet rs = s.executeQuery(SQL);

       //LOOP
       while (rs.next()){

           //get value
           String Artist = rs.getString(1);
           String SongTitle = rs.getString(2);
           String Price = rs.getString(3);
           String Qty = rs.getString(4);



           dth.addRow(new String[]{Artist, SongTitle, Price, Qty});

       }

       return dth;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

}

编辑后的帖子

为什么我不能像这样从个人资料表单中检索用户会话?

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);

See this image

0 个答案:

没有答案