package com.czh.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.*;
/**
* @author 作者 YYD
* @version 创建时间:2016年6月10日 上午10:09:22
* @function 未添加
*/
public class SqlUtil {
/**
* 数据库驱动类名称
*/
private static final String DRIVER = "com.mysql.jdbc.Driver";
/**
* 连接字符串
*/
private static final String URLSTR = "jdbc:mysql://localhost:3306/chatserver";
/**
* 用户名
*/
private static final String USERNAME = "root";
/**
* 密码
*/
private static final String USERPASSWORD = "root";
/**
* 创建数据库连接对象
*/
private Connection connnection = null;
//
// /**
// * 创建PreparedStatement对象
// */
// private Statement preparedStatement = null;
//
// /**
// * 创建CallableStatement对象
// */
// private CallableStatement callableStatement = null;
private PreparedStatement pstmt = null;
private ResultSet resultSet = null;
public static SqlUtil sqlUtil = new SqlUtil();
public static SqlUtil getInstance(){
return sqlUtil;
}
public SqlUtil(){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
e.printStackTrace();
}
}
/**
* 建立数据库连接
*
* @return 数据库连接
*/
private Connection getConnection() {
try {
// 获取连接
connnection = DriverManager.getConnection(URLSTR, USERNAME,
USERPASSWORD);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return connnection;
}
// /**
// * insert update delete SQL语句的执行的统一方法
// *
// * @param sql
// * SQL语句
// * @param params
// * 参数数组,若没有参数则为null
// * @return 受影响的行数
// */
// public int executeUpdate(String sql, Object[] params) {
// // 受影响的行数
// int affectedLine = 0;
//
// try {
// // 获得连接
// connnection = this.getConnection();
// // 调用SQL
// preparedStatement = connnection.prepareStatement(sql);
//
// // 参数赋值
// if (params != null) {
// for (int i = 0; i < params.length; i++) {
// preparedStatement.setObject(i + 1, params[i]);
// }
// }
//
// // 执行
// affectedLine = preparedStatement.executeUpdate();
//
// } catch (SQLException e) {
// System.out.println(e.getMessage());
// } finally {
// // 释放资源
// closeAll();
// }
// return affectedLine;
// }
public boolean isHaveUser(String userParams,String pwdParams){
try {
// 获得连接
connnection = this.getConnection();
String sqlExe="select * from users where username="+"'"+userParams+"'"+" and password="+"'"+pwdParams+"'";
pstmt = connnection.prepareStatement(sqlExe);
// 执行
resultSet = pstmt.executeQuery();
if (resultSet.next()) {
String userName = resultSet.getString(1);
String pwd = resultSet.getString(2);
if(userParams.equals(userParams)){
return true;
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
closeAll();
}
return false ;
}
// /**
// * SQL 查询将查询结果:一行一列
// *
// * @param sql
// * SQL语句
// * @param params
// * 参数数组,若没有参数则为null
// * @return 结果集
// */
// public Object executeQuerySingle() {
// Object object = null;
// try {
// // 获得连接
// connnection = this.getConnection();
// String sqlExe="select * from user where userName=? and password=?";
// pstmt = connnection.prepareStatement(sqlExe);
//
// // 执行
// ResultSet resultSet = pstmt.executeQuery();
//
// if (resultSet.next()) {
// object = resultSet.getObject(1);
// }
//
// } catch (SQLException e) {
// System.out.println(e.getMessage());
// } finally {
// closeAll();
// }
//
// return object;
// }
//
// /**
// * 获取结果集,并将结果放在List中
// *
// * @param sql
// * SQL语句
// * @return List 结果集
// */
// public List<Object> excuteQuery(String sql, Object[] params) {
// // 执行SQL获得结果集
// ResultSet rs = executeQueryRS(sql, params);
//
// // 创建ResultSetMetaData对象
// ResultSetMetaData rsmd = null;
//
// // 结果集列数
// int columnCount = 0;
// try {
// rsmd = rs.getMetaData();
//
// // 获得结果集列数
// columnCount = rsmd.getColumnCount();
// } catch (SQLException e1) {
// System.out.println(e1.getMessage());
// }
//
// // 创建List
// List<Object> list = new ArrayList<Object>();
//
// try {
// // 将ResultSet的结果保存到List中
// while (rs.next()) {
// Map<String, Object> map = new HashMap<String, Object>();
// for (int i = 1; i <= columnCount; i++) {
// map.put(rsmd.getColumnLabel(i), rs.getObject(i));
// }
// list.add(map);
// }
// } catch (SQLException e) {
// System.out.println(e.getMessage());
// } finally {
// // 关闭所有资源
// closeAll();
// }
//
// return list;
// }
//
// /**
// * 存储过程带有一个输出参数的方法
// *
// * @param sql
// * 存储过程语句
// * @param params
// * 参数数组
// * @param outParamPos
// * 输出参数位置
// * @param SqlType
// * 输出参数类型
// * @return 输出参数的值
// */
// public Object excuteQuery(String sql, Object[] params, int outParamPos,
// int SqlType) {
// Object object = null;
// connnection = this.getConnection();
// try {
// // 调用存储过程
// callableStatement = connnection.prepareCall(sql);
//
// // 给参数赋值
// if (params != null) {
// for (int i = 0; i < params.length; i++) {
// callableStatement.setObject(i + 1, params[i]);
// }
// }
//
// // 注册输出参数
// callableStatement.registerOutParameter(outParamPos, SqlType);
//
// // 执行
// callableStatement.execute();
//
// // 得到输出参数
// object = callableStatement.getObject(outParamPos);
//
// } catch (SQLException e) {
// System.out.println(e.getMessage());
// } finally {
// // 释放资源
// closeAll();
// }
//
// return object;
// }
//
/**
* 关闭所有资源
*/
private void closeAll() {
// 关闭结果集对象
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
// 关闭PreparedStatement对象
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
// // 关闭CallableStatement 对象
// if (callableStatement != null) {
// try {
// callableStatement.close();
// } catch (SQLException e) {
// System.out.println(e.getMessage());
// }
// }
// 关闭Connection 对象
if (connnection != null) {
try {
connnection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于netty的服务器客户端收发消息代码
共37个文件
java:11个
class:11个
jar:6个
4星 · 超过85%的资源 需积分: 50 144 下载量 195 浏览量
2016-06-11
06:54:00
上传
评论 1
收藏 7.64MB ZIP 举报
温馨提示
刚学netty ,写了一个基于netty的服务器客户端收发消息代码,功能非常简单,服务器每3秒向服务器发消息,服务器再把消息反给你。简单收1分,希望大家谅解。
资源推荐
资源详情
资源评论
收起资源包目录
workspace.zip (37个子文件)
ChatServer
bin
com
czh
server
SimpleChatServerInitializer.class 2KB
SimpleChatServer.class 3KB
SimpleChatServerHandler.class 4KB
msg
Message.class 1KB
main
Main.class 542B
util
SqlUtil.class 3KB
config.properties 10B
ReadConfig.class 1KB
.settings
org.eclipse.core.resources.prefs 55B
org.eclipse.jdt.core.prefs 598B
src
com
czh
server
SimpleChatServerInitializer.java 938B
SimpleChatServerHandler.java 2KB
SimpleChatServer.java 2KB
msg
Message.java 1KB
main
Main.java 418B
util
SqlUtil.java 7KB
config.properties 10B
ReadConfig.java 650B
.project 386B
.classpath 581B
libs
netty-all-4.0.36.Final.jar 2.09MB
netty-all-4.0.36.Final-sources.jar 1.68MB
mysql-connector-java-5.1.18-bin.jar 771KB
ChatClient
bin
com
czh
client
SimpleChatClientInitializer.class 2KB
SimpleChatClientHandler.class 1KB
SimpleChatClient.class 2KB
main
Main.class 555B
.settings
org.eclipse.jdt.core.prefs 598B
src
com
czh
client
SimpleChatClientInitializer.java 992B
SimpleChatClientHandler.java 513B
SimpleChatClient.java 1KB
main
Main.java 326B
.project 386B
.classpath 500B
libs
msg.jar 921B
netty-all-4.0.36.Final.jar 2.09MB
netty-all-4.0.36.Final-sources.jar 1.68MB
共 37 条
- 1
资源评论
- trainli2019-04-07又是网上的例子,没意思
- qq_310146132016-12-15又是网上的例子,没意思
- marila47202018-01-26不错的资源
- killeraction2018-05-02又是网上的例子,没意思
程序编织梦想
- 粉丝: 85
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功