package mysql;
創(chuàng)新互聯(lián)公司成立與2013年,先為鄭州等服務(wù)建站,鄭州等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為鄭州企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
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ù)庫(kù)連接成功");
} else {
System.out.println("數(shù)據(jù)庫(kù)連接失敗");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 實(shí)例化 Statement 對(duì)象
Statement statement = getConnection().createStatement();
// 要執(zhí)行的 Mysql 數(shù)據(jù)庫(kù)操作語(yǔ)句(增、刪、改、查)
String sql = "";
// 展開(kāi)結(jié)果集數(shù)據(jù)庫(kù)
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通過(guò)字段檢索
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)容可就麻煩了,有可能的話(huà)你看看Java的一個(gè)開(kāi)源庫(kù)Lucene。
要是簡(jiǎn)單的搜文件名包含的字符串,大致應(yīng)該涉及到文件樹(shù)的遍歷算法,最多用一些簡(jiǎn)單的正則表達(dá)式來(lái)匹配文件名,一般用遞歸可以實(shí)現(xiàn)任意級(jí)目錄樹(shù)的搜索。
給你個(gè)簡(jiǎ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,舉個(gè)例子:
F:\download
vod.*.exe
運(yùn)行效果如下:
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("回車(chē)");
}
}
}
}
1 首先確定你要搜索的目錄
2 要搜索的關(guān)鍵字 如“花”
3 只搜索圖片類(lèi)型 .jpg .gif .png .bmp之類(lèi)的
4 在文本框里獲得搜索的關(guān)鍵字
5 取得要搜索目錄下的所有圖片類(lèi)型的名字
6 用關(guān)鍵字和取得的文件名一一進(jìn)行對(duì)比
7 若有關(guān)鍵字 記錄該圖片的名字
8 若都沒(méi)關(guān)鍵字 表示無(wú)該名字的圖片
9 空白區(qū)域你可以用一個(gè)窗體來(lái)表示
10 把搜索到的圖片都顯示在這個(gè)窗體。
當(dāng)前名稱(chēng):java檢索的代碼,并查集java實(shí)現(xiàn)
文章出自:http://jinyejixie.com/article48/dsedgep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、定制網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、App開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)