2011年7月25日 星期一

[java] (5) catch data from database


/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/



/*

* test2.java

*

* Created on 2011/7/11, 下午 11:39:40

*/

package test;





import java.sql.*;



/**

*

* @author luke

*/

public class test31 extends javax.swing.JFrame {



/** Creates new form test2 */

public test31() {

initComponents();

}



/** 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")

//

private void initComponents() {



data_TextField1 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

Sale_jTextField1 = new javax.swing.JTextField();

jLabel3 = new javax.swing.JLabel();

Total_money_jTextField2 = new javax.swing.JTextField();



setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



data_TextField1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

data_TextField1ActionPerformed(evt);

}

});



jButton1.setText("送出");

jButton1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

jButton1ActionPerformed(evt);

}

});



jLabel1.setText("貨名");



jLabel2.setText("銷貨");



jLabel3.setText("小計");



javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel3, 0, 0, Short.MAX_VALUE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(Total_money_jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(58, 58, 58))

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)

.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(data_TextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(Sale_jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(40, 40, 40)))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(44, 44, 44))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(data_TextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(Sale_jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(Total_money_jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))

.addContainerGap())

);



pack();

}//




private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

Connection conn = null;

String url = "jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=big5";

String user = "testuser";

String password = "test623";



try {



conn = DriverManager.getConnection(url, user, password);



Statement st = conn.createStatement();

String Data_text = data_TextField1.getText();

String Sale_text = Sale_jTextField1.getText();





// 如果 Sale_jTextField1 沒有輸入數值的話的判斷式

if ( Sale_text.length() == 0 || Sale_text == null ){

Sale_text = Integer.toString(0);

}







// st.executeUpdate("INSERT INTO tabaco (Name,Sale) VALUES ('"+ Data_text+"','"+ Sale_text +"')");





//去資料庫抓原有庫存數量

ResultSet rs = st.executeQuery("SELECT Upper_stack from tabaco where Name = '" + Data_text + "'");

String SQ = "0";

if(rs.next()){

SQ =rs.getString("Upper_stack");

}

System.out.println(SQ);








data_TextField1.setText("");

Sale_jTextField1.setText("");





st.close();

conn.close();



} catch (SQLException ex) {

System.out.println(ex.getMessage());

}



}



private void data_TextField1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

}



/**

* @param args the command line arguments

*/

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {



public void run() {

new test31().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JTextField Sale_jTextField1;

private javax.swing.JTextField Total_money_jTextField2;

private javax.swing.JTextField data_TextField1;

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

// End of variables declaration

}



Reference :



  • http://www.javaworld.com.tw/jute/post/view?bid=6&id=133307&tpg=1&ppg=1&sty=1&age=0#133307


0 意見:

張貼留言