关于我常用的一些工具类

原创 2016年08月30日 00:03:39
下面我就把我最近在写的项目,自己封装起来的工具类贴出来,本人是个菜鸟,还望大神不要喷,注释也写得不是很清楚,我也懒得在上面加注释了。下面贴出的工具类,根据我的理解,主要用于JAVA操作数据库的一个工具类。需要一个jar包,如果大家需要的话,可以去我的百度云下载,现在我设个密码,大家可以加我QQ331534013,当加我的人多了,我就不设密码了。下面这个是封装JDBC的一个工具类,
<pre name="code" class="java">package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 关闭资源封装工具类
 * 
 * @author 梁智勇
 */
public class JdbcUtils {
	public static void main(String[] args) throws SQLException {
		JdbcUtils.getConnection();
	}
	private static Config cf = new Config("config/config.properties");
	private static String classDriver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/jrs_qms_llh";
	private static String user = "root";
	private static String password = "123456";

	static {
		classDriver = cf.get("classDriver");
		url = cf.get("url");
		user = cf.get("user");
		password = cf.get("password");
		try {
			Class.forName(classDriver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 封装更新方法
	 * 
	 * @throws SQLException
	 */
	public static int executeUpdate(Connection c, String sql, Object[] o)
			throws SQLException {
		PreparedStatement psStatement = null;
		try {

			psStatement = c.prepareStatement(sql);
			if (null != o && o.length > 0) {
				for (int i = 0; i < o.length; i++) {
					psStatement.setObject(i + 1, o[i]);
				}
			}
			int rows = psStatement.executeUpdate();
			return rows;
		} finally {
			close(psStatement);
		}
	}

	/** 封装查询方法 */
	public static List<Object[]> executeQuery(Connection c, String sql,
			Object[] o) throws SQLException {
		PreparedStatement psStatement = null;
		List<Object[]> list = new ArrayList<Object[]>();
		try {
			psStatement = c.prepareStatement(sql);
			if (null != o && o.length > 0) {
				for (int i = 0; i < o.length; i++) {
					psStatement.setObject(i + 1, o[i]);
				}
			}
			ResultSet r = psStatement.executeQuery();
			int colcount = r.getMetaData().getColumnCount(); // 获得结果列的数量
			while (r.next()) {
				Object[] oArr = new Object[colcount];
				for (int i = 0; i < oArr.length; i++) {
					oArr[i] = r.getObject(i + 1);
				}
				list.add(oArr);
			}
			return list;
		} finally {
			close(psStatement);
			close(c);
		}
	}

	/** 使用封装查询方法 */
	public static <T> List<T> executeQuery(Connection c, String sql,
			Object[] o, RowMapper<T> rm) throws SQLException {
		PreparedStatement psStatement = null;
		List<T> list = new ArrayList<T>();
		try {
			psStatement = c.prepareStatement(sql);
			if (null != o && o.length > 0) {
				for (int i = 0; i < o.length; i++) {
					psStatement.setObject(i + 1, o[i]);
				}
			}
			ResultSet r = psStatement.executeQuery();
			int colcount = r.getMetaData().getColumnCount(); // 获得结果列的数量
			while (r.next()) {
				T t = rm.getEntity(r);
				list.add(t);
			}
			return list;
		} finally {
			close(psStatement);
			close(c);
		}
	}

	/** 获得连接方法 */
	public static Connection getConnection() throws SQLException {
		Connection c = DriverManager.getConnection(url, user, password);
		return c;
	}

	/** 关闭ResultSet */
	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/** 关闭Statement */
	public static void close(Statement st) {
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/** 关闭Connection */
	public static void close(Connection c) {
		if (c != null) {
			try {
				c.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/** 关闭ResultSet ,Statement ,Connection */
	public static void close(ResultSet rs, Statement st, Connection c) {
		close(rs);
		close(st);
		close(c);
	}
}




下面是配置文件工具类

package util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class Config {
	private Properties properties = new Properties();

	public Config(String path)  {
		InputStream in = null;

			try {
				in = new FileInputStream(path);
				properties.load(in);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
	}
	
	public String get(String key){
		return properties.getProperty(key);
	}
}
将数据库返回的信息变成对象

package util;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *	将ResulitSet中的一行转化成实体对象
 */
public interface RowMapper<T> {
	public T getEntity(ResultSet rs) throws SQLException;
}


以及做排队叫号系统用到的一个叫号工具类

package util;

import java.io.FileInputStream;
import java.io.IOException;

import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class SoundUtil {
	public static synchronized void playSound(String ss, String winF)
			throws IOException, InterruptedException {
		// System.out.println("1");
		FileInputStream fileau;
		fileau = new FileInputStream("sound\\please.wav");
		AudioStream as = new AudioStream(fileau);
		AudioPlayer.player.start(as);
		Thread.sleep(500);

		for (int i = 0; i < ss.length(); i++) {
			fileau = new FileInputStream("sound\\" + ss.charAt(i) + ".wav");
			as = new AudioStream(fileau);
			AudioPlayer.player.start(as);
			Thread.sleep(500);
		}

		fileau = new FileInputStream("sound\\dao.wav");
		as = new AudioStream(fileau);
		AudioPlayer.player.start(as);
		Thread.sleep(500);

		for (int i = 0; i < winF.length(); i++) {
			fileau = new FileInputStream("sound\\" + winF.charAt(i) + ".wav");
			as = new AudioStream(fileau);
			AudioPlayer.player.start(as);
			Thread.sleep(500);
		}

		fileau = new FileInputStream("sound\\last.wav");
		as = new AudioStream(fileau);
		AudioPlayer.player.start(as);
		Thread.sleep(2000);
	}

	public static void main(String[] args) {

		try {
			SoundUtil.playSound("A2015", "8");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

java常用的26个工具类

  • 2017-09-29 16:57
  • 3.25MB
  • 下载

Android常用工具类

Android常用工具类 分类: Android 2013-02-21 15:13 107人阅读 评论(0) 收藏 举报 读取流文件 StreamTool.java ...

常用时间转换工具类

JDBC常用工具类

package com.sj.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr...

android 常用工具类

  • 2015-12-14 21:30
  • 43KB
  • 下载

ffmpeg视频上传功能常用的俩个工具类【1.视频转码=2.视频抓图】

我们在用ffmpeg做视频上传功能时,经常会用到视频的转码和视频抓图的俩个功能,这里贴出俩个常用的工具类代码 1.ffmpeg视频转码,多数在做视频上传功能时,会将视频转为flv格式,因为flv体积...

常用的android 工具类

  • 2016-01-18 14:55
  • 39KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)