package mysql;

創(chuàng)新互聯(lián)公司成立與2013年,先為鄭州等服務(wù)建站,鄭州等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為鄭州企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("數(shù)據(jù)庫連接成功");
} else {
System.out.println("數(shù)據(jù)庫連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 實例化 Statement 對象
Statement statement = getConnection().createStatement();
// 要執(zhí)行的 Mysql 數(shù)據(jù)庫操作語句(增、刪、改、查)
String sql = "";
// 展開結(jié)果集數(shù)據(jù)庫
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通過字段檢索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 輸出數(shù)據(jù)
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次關(guān)閉
resultSet.close();
statement.close();
getConnection().close();
}
}
你是搜文件名,還是搜文件內(nèi)容?要是搜文件內(nèi)容可就麻煩了,有可能的話你看看Java的一個開源庫Lucene。
要是簡單的搜文件名包含的字符串,大致應(yīng)該涉及到文件樹的遍歷算法,最多用一些簡單的正則表達式來匹配文件名,一般用遞歸可以實現(xiàn)任意級目錄樹的搜索。
給你個簡單的版本吧:
package?test.tool;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.io.IOException;
import?java.util.regex.Matcher;
import?java.util.regex.Pattern;
public?class?FindFile?{
private?String?fileName?=?"";
private?String?dir?=?"";
private?Matcher?m?=?null;
private?int?count?=?0;
public?FindFile()?throws?IOException?{
String?f?=?FindFile.class.getResource("findfile.properties").getFile();
BufferedReader?read?=?new?BufferedReader(new?FileReader(f));
dir?=?read.readLine().trim();
fileName?=?read.readLine().trim();
Pattern?p?=?Pattern.compile(fileName);
m?=?p.matcher("");
}
public?void?find()?{
File?root?=?new?File(dir);
for?(File?f?:?root.listFiles())?{
if?(f.isDirectory())?{
dir?=?f.getAbsolutePath();
find();
}?else?{
m.reset(f.getName());
if?(m.find())?{
count++;
System.out.println(f.getAbsolutePath());
}
}
}
}
public?static?void?main(String[]?args)?{
try?{
FindFile?ff?=?new?FindFile();
ff.find();
System.out.println("\n共找到文件數(shù)目:"?+?ff.count);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
里面用到的findfile.properties,舉個例子:
F:\download
vod.*.exe
運行效果如下:
F:\download\firefox\vodplayer.exe
F:\download\ie\vodplayer.exe
共找到文件數(shù)目:2
import ja.io.*;
//獲取文件夾內(nèi)容
public class getthing
{
public static void main(String[] args) throws Exception
{
System.out.println(welstr);
listFile(new File("e:\\aa")); //想要搜索的路徑
}
public static void listFile(File file) throws Exception
{
if(file.isFile())
{
//輸出的是完整的文件夾內(nèi)文件的路徑
System.out.println("File :"+file.getAbsolutePath());
//01.jpg就是你要找的圖片
if (file.getAbsolutePath().endsWith("01.jpg"))
System.out.println("有搜索的圖片");
}
else
{
System.out.println("Dir :"+file.getAbsolutePath());
File[] files =file.listFiles();
for(int i=0;ifiles.length;i++)
{
listFile(files[i]);
System.out.println("回車");
}
}
}
}
1 首先確定你要搜索的目錄
2 要搜索的關(guān)鍵字 如“花”
3 只搜索圖片類型 .jpg .gif .png .bmp之類的
4 在文本框里獲得搜索的關(guān)鍵字
5 取得要搜索目錄下的所有圖片類型的名字
6 用關(guān)鍵字和取得的文件名一一進行對比
7 若有關(guān)鍵字 記錄該圖片的名字
8 若都沒關(guān)鍵字 表示無該名字的圖片
9 空白區(qū)域你可以用一個窗體來表示
10 把搜索到的圖片都顯示在這個窗體。
當前名稱:java檢索的代碼,并查集java實現(xiàn)
文章出自:http://chinadenli.net/article48/dsedgep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制網(wǎng)站、移動網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、App開發(fā)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)