package .....;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class ConnectionUtils {
private static String url;
private static String driver;
private static String username;
private static String password;
static {
Properties props = new Properties();
try {
// 从属性文件中读取数据库配置信息
props.load(ConnectionUtils.class.getClassLoader()
.getResourceAsStream("homework/db_oracle.properties"));
} catch (IOException e) {
}
if (props != null) {
url = props.getProperty("url");
driver = props.getProperty("driver");
username = props.getProperty("username");
password = props.getProperty("password");
// 装载并注册数据库驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
}
}
}
public static Connection openConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
public static void closeConnection(Connection con) {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}
public static void closeStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
}
}
public static void closeResultSet(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}
}
|
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTestHarnessV5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.printf("%nEnter your regex: ");
Pattern pattern = Pattern.compile(scanner.nextLine());
System.out.printf("Enter input string to search: ");
Matcher matcher = pattern.matcher(scanner.nextLine());
boolean found = false;
while (matcher.find()) {
System.out.printf("Found \"%s\" starting index %d ending index %d.%n",
matcher.group(), matcher.start(), matcher.end());
found = true;
}
if (!found) {
System.out.printf("No match found.%n");
}
}
}
}
|