十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
你这跟struts2没关系,需要一个定时器,定时去跑你的查询库表代码就OK了。
在定陶等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站设计 网站设计制作专业公司,公司网站建设,企业网站建设,成都品牌网站建设,网络营销推广,外贸营销网站建设,定陶网站建设费用合理。
如果你的程序是需要将查询结果展示在jsp页面上,你需要在jsp页面上使用js写定时器代码,参考SetInterval方法。
如果你的程序是桌面应用程序,你可以使用java的定时器Timer。当然,你的项目中如果使用了spring,那有更好的解决方案Quartz。
具体问题要具体分析了。
我用的是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 ListObject list = new ArrayListObject();
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, ListObject 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 ListObject data;
public BaseTableModel(String[] cols, ListObject 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;
}
}
根据选择查询内容在后台动态拼接sql,比如条件A,select * from A,条件B换成select * from B.
前端也可以动态的显示要查询的内容。