`
王世伟
  • 浏览: 24666 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
数据库的连接
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");
			}			
		}
	}
}
MD5摘要
package com.tarena.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
  /**
   * 将原文str经过MD5摘要算法得到密文
   * @param str 如: 1234
   * @return "1234" 的摘要
   */
  public static String md5(String str){
    try {
      MessageDigest md = 
        MessageDigest.getInstance("MD5");
      md.update(str.getBytes());
      byte[] md5 = md.digest();
      char[] ch = "0123456789abcdef".toCharArray();
      StringBuilder buf = new StringBuilder();
      for (byte b : md5) {
        buf.append(ch[ b>>>4 & 0xf ]);
        buf.append(ch[ b& 0xf ]);
      }
      return buf.toString();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }
  
  public static void main(String[] args) {
    System.out.println(md5("1234"));
  }
}







Global site tag (gtag.js) - Google Analytics