class WindowEventHandler extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.out.println("系统退出:exit()"); System.exit(0); } }
class ActionEventHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ System.out.println ("**********"); int i = Integer.parseInt(ta.getText()); String str = tb.getText(); UpdateSql(i, str); } }
public void UpdateSql(int i, String str){ try{ String sqlUp =""; if(SelectSql(i)){ System.out.println ("Update"); sqlUp = "update aa set name='"+str+"' where num="+i; }else{ System.out.println ("Insert"); sqlUp = "insert into aa values("+i+",'"+str+"')"; }
Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection("jdbc:oracle:thin:@172.17.93.117:1521:ora","scott","tiger"); sta = conn.createStatement(); res = sta.executeUpdate(sqlUp); System.out.println ("===========: "+res);
import java.awt.*;
import java.awt.event.*;public class OraUpdate extends Frame{
private Panel p = null;
private TextField ta = null;
private TextField tb = null;
private Button btnOK = null;
Connection conn = null;
Statement sta = null;
ResultSet rs = null;
int res = 0;
public OraUpdate(){
p = new Panel();
ta = new TextField(4);
tb = new TextField(10);
btnOK = new Button(" OK ");
add(p);
p.add(ta);
p.add(tb);
p.add(btnOK);
addWindowListener(new WindowEventHandler());
btnOK.addActionListener(new ActionEventHandler());
setTitle("Ora Control");
setSize(300,100);
show();
}
class WindowEventHandler extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.out.println("系统退出:exit()");
System.exit(0);
}
}
class ActionEventHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
System.out.println ("**********");
int i = Integer.parseInt(ta.getText());
String str = tb.getText();
UpdateSql(i, str);
}
}
public void UpdateSql(int i, String str){
try{
String sqlUp ="";
if(SelectSql(i)){
System.out.println ("Update");
sqlUp = "update aa set name='"+str+"' where num="+i;
}else{
System.out.println ("Insert");
sqlUp = "insert into aa values("+i+",'"+str+"')";
}
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@172.17.93.117:1521:ora","scott","tiger");
sta = conn.createStatement();
res = sta.executeUpdate(sqlUp);
System.out.println ("===========: "+res);
conn.commit();
sta.close();
conn.close();
}catch(Exception e){
System.out.println ("++"+e);
}
}
public boolean SelectSql(int i){
try{
String sqlSel = "select "+i+" from aa";
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@172.17.93.117:1521:ora","scott","tiger");
sta = conn.createStatement();
rs = sta.executeQuery(sqlSel);
while(rs.next()){
rs.close();
sta.close();
conn.close();
System.out.println ("@@@@@@@@@true");
return true;
}
rs.close();
sta.close();
conn.close();
}catch(Exception e){
System.out.println ("++"+e);
}
System.out.println ("@@@@@@@@@False");
return false;
}
/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args) {
new OraUpdate();
}
}
基本上是这样的,我用的是ACCESS数据库,但是有的地方还不很完善,仅供参考~~
JButton button = new JButton("write in");
button.addMouseListener(new MouseListener(){
public void MousePressed(){
String content = text.getText();
if(content != null){
Statement stmt = conn.createStatement();
stmt.execute("insert into sqltable values( " + content = ")");
}
}
})