「java实现搜索功能」java搜索文件方法
本篇文章给大家谈谈java实现搜索功能,以及java搜索文件方法对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java编写的GUI 怎么实现查找功能(使用搜索
java编写的GUI 怎么实现查找功能:
package communitys.Connect;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class dxdsy extends JFrame implements ActionListener{
private JButton button = new JButton("搜索");
private JTextField textfile = new JPasswordField("请输入文件名称······");
public dxdsy()
{
this.setLayout(null);
this.setBounds(200,200, 500,500);
textfile.setBounds(1, 1, 100,20);
button.setBounds(1, 25, 80,80);
this.add(button);
this.add(textfile);
button.addActionListener(this);//添加事件监听
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
String sql = "select * from tablename where 条件 like '%"+textfile.getText()+"%'";
try {
Class.forName("驱动字符");
Connection conn = DriverManager.getConnection("驱动字符");
Statement sta = conn.createStatement();
ResultSet rs = sta.executeQuery(sql);
//这个rs集合当中就是想要的数据
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
java如何实现搜索功能。比如,输入txt就能搜索出这个文件夹内所有txt格式的文件。请给完整代码。
import java.io.*;
public class FileDemo{
public static void main(String[] args)throws Exception{
//第一个参数是文件路径,第二个参数是要搜索的文件扩展名
getFile("D:\\JavaDemo",".txt");
}
private static void getFile(String pathName, final String endsWith)throws Exception{
File file = new File(pathName);
if(!file.exists())
throw new RuntimeException("文件不存在,你检索个P呀。");
file.listFiles(new FileFilter(){
public boolean accept(File file){
if(file.getName().endsWith(endsWith)){
System.out.println(file.getName());
return true;
}else
return false;
}
});
}
}
用Java如何实现搜索功能
web程序的话如下:
画一个页面
表单提交用户输入数据
控制器接收提交到业务层
业务层处理返回页面
搜索实际上就是一个查询 select * from XXX
关于java实现搜索功能和java搜索文件方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。