MYSQL与SQL可视化增,删,改1.建立表格:属性:public JTable table;public DefaultTableModel dftm;public String[] columnNames= new String[] { "序号", "用户名", "密码"};// 定义数组表示表格标题dftm = new DefaultTableModel(null, columnNames);/*for (int i = 0; i < 10; i++) {Object[] content = { 1, 2, 3 };dftm.addRow(content);// 增加行}*/initTable(dftm);JTable table = new JTable(dftm);// 根据TableModel显示内容JScrollPane src = new JScrollPane(table); //给表格增加滚动条src.setBounds(50, 150, 500, 200); //设置滚动条位置与大小contentPane.add(src);2.“修改用户”按钮的代码:JButton rewamp = new JButton("\u4FEE\u6539\u7528\u6237");rewamp.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {int id=Integer.parseInt(txtId.getText());String name=txtName.getText();//取得用户名文本框的值String pwd=txtPwd.getText();//取得用户密码文本框的值String sqlUpdate="UPDATE userlist set name='" +name+"', password='"+pwd+"' where Id="+id;DBComm dbco=new DBComm();dbco.openConnection();try {dbco.executeUpdate(sqlUpdate);dbco.closeConnection();initTable(dftm);} catch (SQLException e1) {e1.printStackTrace();}}});rewamp.setBounds(338, 24, 116, 28);contentPane.add(rewamp);3.“删除用户”按钮的代码:JButton delete = new JButton("\u5220\u9664\u7528\u6237");delete.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (JOptionPane.showConfirmDialog(null,"您确定要删除ID为" + txtId.getText() + "的用户吗?","",0) == 0) {int id = Integer.parseInt(txtId.getText());String sqldel = "delete from userlist where id="+ id;try {DBComm dbco = new DBComm();dbco.openConnection();dbco.executeUpdate(sqldel);dbco.closeConnection();initTable(dftm);} catch (SQLException e1){e1.printStackTrace();}}}});delete.setBounds(198, 24, 116, 28);contentPane.add(delete);4.“增加用户”按钮的代码:JButton add = new JButton("\u589E\u52A0\u7528\u6237");add.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String addId=txtId.getText();String addName=txtName.getText();String addPwd=txtPwd.getText();String strlns="insert into userlist(name,password) values('"+addName+"','"+addPwd+"')";//strlns.format( addName,addPwd);DBComm dbco=new DBComm();dbco.openConnection();dbco.executeUpdate(strlns);try {initTable(dftm);} catch (SQLException e1) {e1.printStackTrace();}}});add.setBounds(46, 24, 116, 28);contentPane.add(add);5.选择表格的当前行,获得行中信息:table.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {String id, uName, passstr;int selRow = table.getSelectedRow();//获得表格当前选中行编号id = table.getValueAt(selRow, 0).toString().trim();//去掉空格,并变成字符串型uName = table.getValueAt(selRow, 1).toString().trim();passstr = table.getValueAt(selRow, 2).toString().trim();txtId.setText(id);txtName.setText(uName);txtPwd.setText(passstr);}});}6.设置dftm里面的内容:public void initTable(DefaultTableModel dftm) throws SQLException{ dftm.setDataVector(null, columnNames);//。
可以使表格重新加载DBComm dbco=new DBComm(); //自定义的数据操作通用类dbco.openConnection(); //进行数据库连接ResultSet rs=dbco.executeQuery("select * from userlist");//取得数据表中所有数据while(rs.next()){Object[] content = { rs.getObject(1), rs.getObject(2), rs.getObject(3) };dftm.addRow(content);// 增加行}dbco.closeConnection();}运行结果:DBComm连接类package tool;import java.sql.*;public class DBComm {public String DBDRIVER = null;//DBDRIVER数据库驱动字符串private String connectionString = null;public String passWord = null;public Connection conn;public Statement stmt;public PreparedStatement pstmt;/*** 数据库操作通用公共类* DBDRIVER数据库驱动字符串* connectionString数据库连接字符串* userName连接数据库用户名* passWord连接数据库密码* conn数据库连接对象* stmt数据库操作对象*/SQL连接属性赋值:加载驱动:jdbc4.jarpublic DBComm() {DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";connectionString="jdbc:sqlserver://localhost:1433;DatabaseName=Eshop";userName = "sa";passWord = "sa";}MYSQL连接属性赋值:加载驱动:mysql-connector-java-bin.jarpublic DBComm() {DBDRIVER = "com.mysql.jdbc.Driver";connectionString="jdbc:mysql://localhost/Login?useUnicode=true&characterEncoding=GBK";userName = "root";passWord = "null";}/*** 打开数据库连接* @return数据库是否成功连接*/public boolean openConnection() {boolean result = true;try {Class.forName(DBDRIVER);if (userName == null && passWord == null)conn = DriverManager.getConnection(connectionString);elseconn=DriverManager.getConnection(connectionString, userName,passWord);stmt = conn.createStatement();result = true;} catch (Exception e) {e.printStackTrace();result = false;}return result;}/*** 关闭数据库连接*/public void closeConnection() {try {stmt.close();conn.close();} catch (SQLException e) {e.printStackTrace();}}/*** 执行增、删、改记录操作* @param sql为增、删、改的sql语句* @return受影响的记录数*/public int executeUpdate(String sql) {int result = 0;try {result = stmt.executeUpdate(sql);} catch (SQLException e) {e.printStackTrace();}return result;}public int executeUpdate(String sql, Object[] tmp) throws Exception { int result = 0;pstmt=conn.prepareStatement(sql);if(tmp!=null){for(int i=0;i<tmp.length;i++){pstmt.setObject(i+1, tmp[i]);}}result = pstmt.executeUpdate();return result;}/*** 执行查询数据库操作* @param sql为sql查询语句* @return查询得到的数据集*/public ResultSet executeQuery(String sql) {ResultSet rs = null;try {rs = stmt.executeQuery(sql);} catch (SQLException e) {e.printStackTrace();}return rs;}}。