帮我看看为啥数据修改显示成功但是没成功
程序代码:
package com.library.Dao; //数据库的操作类:驱动程序加载、数据库连接、连接关闭方法 import java.sql.*; public class GetConnection { private Connection conn;//定义连接对象 private String user="sa";//默认用户名 private String password = "199738"; //密码 private String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 private String url = "jdbc:sqlserver://localhost:1433; DatabaseName=library"; //连接服务器和数据库 public GetConnection() { try { Class.forName(className); }catch(ClassNotFoundException e){ System.out.println("数据库连接失败!"); e.printStackTrace(); } } public Connection getCon() { try{ conn=DriverManager.getConnection(url,user,password); }catch(SQLException e) { System.out.println("数据库驱动加载失败!"); e.printStackTrace(); } return conn; } public int executeUpdate(String sql) { try { if(conn==null) new GetConnection(); return conn.createStatement().executeUpdate(sql); } catch (SQLException e) { System.out.println(e.getMessage()); return -1; } finally { } } public ResultSet executeQuery(String sql) { try { if(conn==null) new GetConnection(); return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE).executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); return null; } finally { } } public void closed() { try{ if(conn!=null) conn.close(); }catch(SQLException e) { System.out.println("连接关闭失败!"); e.printStackTrace(); } } } package com.library.Dao; import java.sql.Connection; import com.library.Dao.GetConnection;; public class UpdatebookType { Connection conn=null; public GetConnection connection=new GetConnection(); public int UpdatebookType(String id,String typeName){ int i=0; conn=connection.getCon(); try{ String sql="update tb_bookType set name='"+typeName+"'where type_id='"+id+"'"; i=connection.executeUpdate(sql); }catch(Exception e){ e.printStackTrace(); } connection.closed(); return i; } } package com.library.Dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import com.library.Dao.GetConnection; import com.library.bean.BookType;; public class UpdateBookTypeDao { Connection conn=null; public GetConnection connection=new GetConnection(); public List UpdateBookType(){ List list=new ArrayList(); String sql = "select * from tb_booktype"; conn=connection.getCon(); ResultSet rs = connection.executeQuery(sql); try { while (rs.next()) { BookType bookType=new BookType(); bookType.setType_id(rs.getInt("type_id")); bookType.setName(rs.getString("name")); list.add(bookType); } } catch (Exception e) { e.printStackTrace(); } connection.closed(); return list; } } package com.library.mainFrame; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.JComboBox; import java.awt.Font; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.List; import java.awt.event.ActionEvent; import java.awt.Color; import com.library.bean.BookType; import com.library.bean.Item; import com.library.Dao.UpdateBookTypeDao; import com.library.Dao.UpdatebookType; import javax.swing.JScrollPane; import javax.swing.table.DefaultTableModel; import javax.swing.DefaultComboBoxModel; import javax.swing.ImageIcon; public class UpdateBookType extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private JTextField textField; private JTable table; private JTextField BookTypeId; private String[] columnNames={"图书类别编号", "图书类别名称"}; DefaultTableModel model; DefaultComboBoxModel bookTypeModel; UpdatebookType update=new UpdatebookType(); UpdateBookTypeDao updateBOOK=new UpdateBookTypeDao(); /** * Launch the application. */ private Object[][] getFileStates(List list){ Object[][]results=new Object[list.size()][columnNames.length]; for(int i=0;i<list.size();i++){ BookType booktype=(BookType)list.get(i); results[i][0]=booktype.getType_id(); results[i][1]=booktype.getName(); } return results; } /** * Create the frame. */ public UpdateBookType() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 554, 427); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel label = new JLabel(""); label.setIcon(new ImageIcon(UpdateBookType.class.getResource("/com/library/images/UpdateBookType.jpg"))); label.setBounds(0, 0, 538, 88); contentPane.add(label); JComboBox comboBox = new JComboBox(); bookTypeModel= (DefaultComboBoxModel)comboBox.getModel(); List list=updateBOOK.UpdateBookType(); for(int i=0;i<list.size();i++){ BookType booktype=(BookType)list.get(i); Item item=new Item(); item.setId(booktype.getType_id()); item.setName(booktype.getName()); bookTypeModel.addElement(item); } comboBox.setBounds(388, 165, 121, 30); contentPane.add(comboBox); JLabel label_1 = new JLabel("图书类型:"); label_1.setFont(new Font("宋体", Font.PLAIN, 18)); label_1.setBounds(272, 163, 90, 30); contentPane.add(label_1); JButton btnNewButton = new JButton("修改"); btnNewButton.addActionListener(new ButtonAddListener()); btnNewButton.setFont(new Font("宋体", Font.PLAIN, 12)); btnNewButton.setBounds(120, 337, 93, 23); contentPane.add(btnNewButton); JButton btnNewButton_1 = new JButton("关闭"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { new UpdateBookType(); UpdateBookType.this.dispose(); } }); btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 12)); btnNewButton_1.setBounds(333, 337, 93, 23); contentPane.add(btnNewButton_1); JLabel label_2 = new JLabel("修改类型:"); label_2.setFont(new Font("宋体", Font.PLAIN, 18)); label_2.setBounds(272, 257, 90, 30); contentPane.add(label_2); textField = new JTextField(); textField.setFont(new Font("宋体", Font.PLAIN, 15)); textField.setBounds(388, 259, 121, 30); contentPane.add(textField); textField.setColumns(10); model=new DefaultTableModel(); Object[][] results=getFileStates(updateBOOK.UpdateBookType()); model.setDataVector(results,columnNames); JTable table = new JTable(); table.setModel(model); table.setFont(new Font("宋体", Font.PLAIN, 13)); JScrollPane scrollPane = new JScrollPane(table); scrollPane.setFont(new Font("宋体", Font.PLAIN, 18)); scrollPane.setBackground(Color.WHITE); scrollPane.setBounds(20, 111, 247, 219); contentPane.add(scrollPane); BookTypeId = new JTextField(); BookTypeId.setBounds(388, 208, 121, 30); contentPane.add(BookTypeId); BookTypeId.setColumns(10); } class TableListener extends MouseAdapter { public void mouseClicked(final MouseEvent e) { int selRow = table.getSelectedRow(); BookTypeId.setText(table.getValueAt(selRow, 0).toString().trim()); bookTypeModel.setSelectedItem(table.getValueAt(selRow, 1).toString().trim()); } } class ButtonAddListener implements ActionListener{ public void actionPerformed(ActionEvent e){ Object selectedItem = bookTypeModel.getSelectedItem(); int i=update.UpdatebookType(BookTypeId.getText().trim(),selectedItem.toString()); System.out.println(i); if(i==1){ JOptionPane.showMessageDialog(null, "修改成功!"); } } } }