重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java检索的代码,并查集java实现

java连接数据库的代码

package mysql;

创新互联公司成立与2013年,先为郑州等服务建站,郑州等地企业,进行企业商务咨询服务。为郑州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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("数据库连接成功");

} else {

System.out.println("数据库连接失败");

connection.close();

}

return connection;

}

public void getResult() throws ClassNotFoundException, SQLException {

// 实例化 Statement 对象

Statement statement = getConnection().createStatement();

// 要执行的 Mysql 数据库操作语句(增、删、改、查)

String sql = "";

// 展开结果集数据库

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

// 通过字段检索

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

// 输出数据

System.out.println("ID : " +id);

System.out.println("name :" + name);

}

// 完成后需要依次关闭

resultSet.close();

statement.close();

getConnection().close();

}

}

java搜索文件的代码怎么写,返回文件的路径?求教

你是搜文件名,还是搜文件内容?要是搜文件内容可就麻烦了,有可能的话你看看Java的一个开源库Lucene。

要是简单的搜文件名包含的字符串,大致应该涉及到文件树的遍历算法,最多用一些简单的正则表达式来匹配文件名,一般用递归可以实现任意级目录树的搜索。

给你个简单的版本吧:

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共找到文件数目:" + ff.count);

} catch (IOException e) {

e.printStackTrace();

}

}

}

里面用到的findfile.properties,举个例子:

F:\download

vod.*.exe

运行效果如下:

F:\download\firefox\vodplayer.exe

F:\download\ie\vodplayer.exe

共找到文件数目:2

通过文字搜索图片 java代码实现及说明

import ja.io.*;

//获取文件夹内容

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())

{

//输出的是完整的文件夹内文件的路径

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 要搜索的关键字 如“花”

3 只搜索图片类型 .jpg .gif .png .bmp之类的

4 在文本框里获得搜索的关键字

5 取得要搜索目录下的所有图片类型的名字

6 用关键字和取得的文件名一一进行对比

7 若有关键字 记录该图片的名字

8 若都没关键字 表示无该名字的图片

9 空白区域你可以用一个窗体来表示

10 把搜索到的图片都显示在这个窗体。


文章名称:java检索的代码,并查集java实现
本文来源:http://cqcxhl.com/article/dsedgep.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP