/*
 * 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 test21 extends javax.swing.JFrame {
    /** Creates new form test2 */
    public test21() {
        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();
        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);
            }
        });
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(data_TextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(data_TextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        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";
        String user = "testuser";
        String password = "test623";
        try {
            conn = DriverManager.getConnection(url, user, password);
            Statement st = conn.createStatement();
            String text = data_TextField1.getText(); \\use this to insert data 
            st.executeUpdate("INSERT INTO Authors (Name) VALUES ('"+ text +"')");
            data_TextField1.setText(""); \\ clear data after input
            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 test21().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField data_TextField1;
    private javax.swing.JButton jButton1;
    // End of variables declaration
}
Question :
- I can't input chinese into database 
 
 
0 意見:
張貼留言