至尊宝的传说
我用的是oracle 数据库。查询String sql = "select EMPNO,ENAME,SAL,DEPTNO from emp where ENAME like ?";显示4个字段///主方法import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.swing.JFrame;public class TrendSearch extends JFrame {private javax.swing.JScrollPane jScrollPane1;private javax.swing.JTable jTable1;private javax.swing.JTextField jTextField1;private MyTableModel tableModel;private List<Object> list = new ArrayList<Object>();private Staff staff;private Connection connection = (Connection) OraSqlConnection.getConnection();ResultSet rs = null;/*** @param args*/public TrendSearch() {initComponents();}private void initComponents() {setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jTextField1 = new javax.swing.JTextField();jScrollPane1 = new javax.swing.JScrollPane();jTable1 = new javax.swing.JTable();jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {public void keyReleased(java.awt.event.KeyEvent evt) {jTextField1KeyReleased(evt);}});setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);getContentPane().add(jTextField1, java.awt.BorderLayout.NORTH);tableModel = new MyTableModel(new String[] { "EMPNO", "ENAME", "SAL","DEPTNO" }, new ArrayList());jTable1.setModel(tableModel);jScrollPane1.setViewportView(jTable1);getContentPane().add(jScrollPane1, java.awt.BorderLayout.SOUTH);pack();}private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {// TODO add your handling code here:String s = jTextField1.getText().trim();list.removeAll(list);if (s != null && !"".equals(s)) {PreparedStatement pstmt = null;System.out.println(s);String sql = "select EMPNO,ENAME,SAL,DEPTNO from emp where ENAME like ?";try {pstmt = connection.prepareStatement(sql);pstmt.setString(1, s + "%");rs = pstmt.executeQuery();while (rs.next()) {staff = new Staff();staff.setEMPNO(rs.getString(1));staff.setENAME(rs.getString(2));staff.setSAL(rs.getString(3));staff.setDEPTNO(rs.getString(4));list.add(staff);}tableModel.data = list;tableModel.fireTableDataChanged();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private class MyTableModel extends BaseTableModel {public MyTableModel(String[] cols, List<Object> data) {super(cols, data);// TODO Auto-generated constructor stub}public Object getValueAt(int rowIndex, int columnIndex) {// TODO Auto-generated method stubStaff s = (Staff) list.get(rowIndex);switch (columnIndex) {case 0:return s.getDEPTNO();case 1:return s.getENAME();case 2:return s.getSAL();case 3:return s.getDEPTNO();default:break;}return null;}}public static void main(String[] args) {// TODO Auto-generated method stubTrendSearch ts = new TrendSearch();ts.setVisible(true);}}///tabel 类import java.util.List;import javax.swing.table.AbstractTableModel;public abstract class BaseTableModel extends AbstractTableModel {public String[] cols;public List<Object> data;public BaseTableModel(String[] cols, List<Object> data) {this.cols = cols;this.data = data;}public int getColumnCount() {return cols.length;}public int getRowCount() {return data.size();}public String getColumnName(int colIndex) {return cols[colIndex];}}//javabean 类public class Staff {private String EMPNO;private String ENAME;private String SAL;private String DEPTNO;public String getEMPNO() {return EMPNO;}public void setEMPNO(String empno) {EMPNO = empno;}public String getENAME() {return ENAME;}public void setENAME(String ename) {ENAME = ename;}public String getSAL() {return SAL;}public void setSAL(String sal) {SAL = sal;}public String getDEPTNO() {return DEPTNO;}public void setDEPTNO(String deptno) {DEPTNO = deptno;}}///JDBC类import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/*** @author jiangzongshuai**/public class OraSqlConnection {private static String className = "oracle.jdbc.driver.OracleDriver";private static String url = "jdbc:oracle:thin:@localhost:1521:ORCL";private static String username = "scott";private static String password = "tiger";private static Connection conn = null;public static Connection getConnection() {try {if (conn !=null) return conn;Class.forName(className);conn = DriverManager.getConnection(url, username, password);return conn;} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}}