下面我就把我最近在写的项目,自己封装起来的工具类贴出来,本人是个菜鸟,还望大神不要喷,注释也写得不是很清楚,我也懒得在上面加注释了。下面贴出的工具类,根据我的理解,主要用于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();
}
}
}