为了账号安全,请及时绑定邮箱和手机立即绑定

你好,求指教JAVA 动态搜索(查找)的问题,该怎么实现?

你好,求指教JAVA 动态搜索(查找)的问题,该怎么实现?

慕田峪9158850 2022-05-26 15:11:56
谁能帮个忙写理一个个JAVA,我研究了很久都研究部出来具体就是:用到三层c/s结构 三层服务器一个JTEextField 一个JTableJTexeField输入什么东西,立刻发送至服务器查找数据库具有该关键字的选项并返回客户端显示在 JTable 里就像百度上面的搜索栏 ,只要输入关键字就可以了我用的MySQL谢谢了,解决后有重谢只要写服务端的重要代码就可以了
查看完整描述

1 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

我用的是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 block
e.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 stub
Staff 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 stub
TrendSearch 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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}



查看完整回答
反对 回复 2022-05-30
  • 1 回答
  • 0 关注
  • 176 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信