2011年7月24日 星期日

[java] (first step) insert data into 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 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() {



degreeTextField1 = new javax.swing.JTextField();

jButton1 = new javax.swing.JButton();



setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);



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

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

degreeTextField1ActionPerformed(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(degreeTextField1, 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(degreeTextField1, 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();

插入Laiq這個名稱

st.executeUpdate("INSERT INTO Authors (Name) VALUES ('Laiq')");



st.close();

conn.close();



} catch (SQLException ex) {

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

}



}



private void degreeTextField1ActionPerformed(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 degreeTextField1;

private javax.swing.JButton jButton1;

// End of variables declaration

}

0 意見:

張貼留言