//java腳本代碼eclipse(版本:jre1.8需導入sqljdbc4.jar包)
package ang;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
//import java.util.Vector;
//import java.awt.*;
import javax.swing.*;
public class data extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton add, select, del, update;
JTable table;
Object body[][] = new Object[50][5];
String fields[] = { "編號", "姓名", "性別", "年齡", "地址" };
Connection conn;
Statement st;
ResultSet rs;
JTabbedPane tp;
public data() {
super("數據庫操作");
this.setSize(400, 300);
this.setLocation(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel ps = new JPanel();
add = new JButton("添加");
select = new JButton("查詢");
update = new JButton("更新");
del = new JButton("刪除");
add.addActionListener(this);
select.addActionListener(this);
update.addActionListener(this);
del.addActionListener(this);
ps.add(add);
ps.add(select);
ps.add(update);
ps.add(del);
this.connection();
table = new JTable(body, fields);
tp = new JTabbedPane();
tp.add("S表", new JScrollPane(table));
this.getContentPane().add(tp, "Center");
this.getContentPane().add(ps, "South");
this.setVisible(true);
}
public void connection() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName = studb","sa","svse");
st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch (Exception ex) {
// TODO: handle exception
ex.printStackTrace();
System.out.println("連接錯誤:" + ex.getMessage());
//System.out.println("連接錯誤");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
data dt = new data();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == add) {
add();
}
if (e.getSource() == select) {
select();
}
if (e.getSource() == update) {
update();
}
if (e.getSource() == del) {
del();
}
}
private void del() {
// TODO Auto-generated method stub
try {
int row = table.getSelectedRow();
String str = "delete stuacce where SNO = '" + body[row][0] + "'";
st.executeUpdate(str);
JOptionPane.showMessageDialog(null, "數據已成功刪除");
this.select();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "刪除數據錯誤!");
}
}
private void update() {
// TODO Auto-generated method stub
try {
int row = table.getSelectedRow();
JTextField t[] = new JTextField[8];
t[0] = new JTextField("輸入姓名:");
t[0].setEditable(false);
t[1] = new JTextField();
t[1].setText((String) body[row][1]);
t[2] = new JTextField("輸入性別:");
t[2].setEditable(false);
t[3] = new JTextField();
t[3].setText((String) body[row][2]);
t[4] = new JTextField("輸入年齡:");
t[4].setEditable(false);
t[5] = new JTextField();
t[5].setText((String) body[row][3]);
t[6] = new JTextField("輸入專業:");
t[6].setEditable(false);
t[7] = new JTextField();
t[7].setText((String) body[row][4]);
String but[] = { "確定", "取消" };
int go = JOptionPane.showOptionDialog(null, t, "修改信息", JOptionPane.YES_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, but, but[0]);
if (go == 0) {
String nName = (String) t[1].getText();
String nsex = (String) t[3].getText();
int nage = Integer.parseInt(t[5].getText());
String ndept = (String) t[7].getText();
String str = "update stuacce set sname = '" + nName + "',sex='" + nsex + "',age='" + nage + "',dept='" + ndept
+ "'where sno='" + body[row][0] + "' ";
st.executeUpdate(str);
JOptionPane.showMessageDialog(null, "修改數據成功!");
this.select();
st.executeUpdate(str);
//JOptionPane.showMessageDialog(null, "數據已經成功插入!");
}
} catch (Exception ex) {
// TODO: handle exception
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "更新數據失敗!");
}
}
private void select() {
// TODO Auto-generated method stub
String str = "select * from stuacce";
filltable(str);
}
private void filltable(String str) {
// TODO Auto-generated method stub
try {
for(int x=0;x<body.length;x++){
body[x][0]=null;
body[x][1]=null;
body[x][2]=null;
body[x][3]=null;
body[x][4]=null;
}
int i = 0;
rs = st.executeQuery(str);
while(rs.next()){
body[i][0]=rs.getString("SNO");
body[i][1]=rs.getString("SNAME");
body[i][2]=rs.getString("SEX");
body[i][3]=rs.getString("AGE");
body[i][4]=rs.getString("DEPT");
i=i+1;
}
this.repaint(i);
} catch (Exception ex) {
// TODO: handle exception
ex.printStackTrace();
}
}
private void add() {
// TODO Auto-generated method stub
try {
JTextField t[] = new JTextField[10];
t[0] = new JTextField("輸入學號:");
t[0].setEditable(false);
t[1] = new JTextField();
t[2] = new JTextField("輸入姓名:");
t[2].setEditable(false);
t[3] = new JTextField();
t[4] = new JTextField("輸入性別:");
t[4].setEditable(false);
t[5] = new JTextField();
t[6] = new JTextField("輸入年齡:");
t[6].setEditable(false);
t[7] = new JTextField();
t[8] = new JTextField("輸入專業:");
t[8].setEditable(false);
t[9] = new JTextField();
String but[] = {"確定","取消"};
int go = JOptionPane.showOptionDialog(null, t, "插入信息", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, null, but, but[0]);
if(go == 0){
try {
int nsno = Integer.parseInt(t[1].getText());
String nName = (String)t[3].getText();
String nsex = (String)t[5].getText();
int nage = Integer.parseInt(t[7].getText());
String ndept = (String)t[9].getText();
String str = "insert into stuacce values('"+nsno+"','"+nName+"','"+nsex+"','"+nage+"','"+ndept+"')";
st.executeUpdate(str);
JOptionPane.showMessageDialog(null, "數據已經成功插入!");
this.select();
} catch (Exception ex) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "數據插入失敗!");
}
}
} catch (Exception ex) {
// TODO: handle exception
}
}
}
sql server2008數據庫腳本,需配置好端口設置1433及啟用IP/TCP
use master
go
if exists(select * from sysdatabases where name = 'studb')
drop database studb
go
create database studb
go
use studb
go
if exists(select * from sysobjects where name = 'stuacce')
drop table stuacce
go
create table stuacce
(
sno int ,
sname varchar(8),
sex varchar(2),
age int,
dept varchar(50)
)
go
insert into stuacce values(1,'張三','男',18,'北京海淀區')
insert into stuacce values(2,'李斯','女',23,'北京朝陽區')
select * from stuacce