//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[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