MySql连接数据库和操作(java)
package org.wxd.weixin.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLUtil {
public Connection getConnection(){
Connection conn = null;
String url ="jdbc:mysql://IP地址/数据库";
String user ="用户名";
String password ="密码";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 释放资源
*/
public void releaseResource(Connection conn, PreparedStatement ps,ResultSet rs){
try {
if(null != rs)
rs.close();
if(null != ps)
ps.close();
if(null != conn)
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//保存用户信息
public static void saveWeixinUser(String openId){
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
String sql = "insert into weixin_user(open_id,subscribe_time,subscribe_status) values(?,now(),1)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,null);
}
}
//保存用户签到信息
public static void saveWeixinSign(String openId,int signPoints){
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
String sql = "insert into weixin_sign(open_id,sign_time,sign_points) values(?,now(),?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
ps.setInt(2, signPoints);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,null);
}
}
//更新用户总积分
public static void updateUserPoints(String openId,int signPoints){
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
String sql = "update weixin_user set points=points+? where open_id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, signPoints);
ps.setString(2, openId);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,null);
}
}
//查询用户总积分
public static String selectUserPoints(String openId){
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select points from weixin_user where open_id=?";
String points = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
rs = ps.executeQuery();
if(rs.next()){
points = rs.getString("points");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,rs);
}
return points;
}
//判断用户今天是否签到
public static boolean isTodaySigned(String openId){
boolean result = false;
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select count(*) as signCounts from weixin_sign where open_id=? and date_format(sign_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
rs = ps.executeQuery();
int signCounts = 0;
if(rs.next()){
signCounts = rs.getInt("signCounts");
}
if(1 == signCounts)
result = true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,rs);
}
return result;
}
/**
* 判断用户本周是否第七次签到
*
* @param openId 用户openid
* @param monday 本周周一的日期时间
* @return
*/
public static boolean isSevenSign(String openId,String monday){
boolean result = false;
MySQLUtil mysql = new MySQLUtil();
Connection conn = mysql.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select conut(*) as signCounts from weixin_sign where open_id=? and sign_time between str_to_date(?,'%Y-%m-%d %H:%i:%s') and now()";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, openId);
ps.setString(2, monday);
rs = ps.executeQuery();
int signCounts = 0;
if(rs.next()){
signCounts = rs.getInt("signCounts");
}
if(6 == signCounts)
result = true;
} catch (SQLException e) {
e.printStackTrace();
}finally {
mysql.releaseResource(conn,ps,rs);
}
return result;
}
}
MySql连接数据库和操作(java)的更多相关文章
- Database学习 - mysql 连接数据库 库操作
连接数据库 语法格式: mysql -h 服务器IP -P 端口号 -u用户名 -p密码 --prompt 命令提示符 --delimiter 指定分隔符 示例: mysql -h 127.0.0.1 ...
- go语言入门教程百度网盘 mysql图形化操作与数据导入
mysql图形化操作与数据导入 @author:Davie 版权所有:北京千锋互联科技有限公司 数据库存储技术 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.每个数据库都有一个 ...
- MySQL常用指令,java,php程序员,数据库工程师必备。程序员小冰常用资料整理
MySQL常用指令,java,php程序员,数据库工程师必备.程序员小冰常用资料整理 MySQL常用指令(备查) 最常用的显示命令: 1.显示数据库列表. show databases; 2.显示库中 ...
- zabbix数据库mariadb从服务器迁移到云mysql数据库的操作
zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...
- C语言对mysql数据库的操作
原文:C语言对mysql数据库的操作 这已经是一相当老的话题.不过今天我才首次使用,把今天的一些体会写下来,也许能给一些新手带来一定的帮助,更重要的是供自己今后忘记的怎么使用而进行查阅的! 我们言归正 ...
- MySQL使用和操作总结
简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...
- Linux下MySQL的简单操作
Linux下MySQL的简单操作 更改mysql数据库root的密码 首次进入数据库是不用密码的: [root@localhost ~]# /usr/local/mysql/bin/mysql -ur ...
- MySQL基本简单操作01
MySQL基本简单操作 学会了安装Docker,那么就将它利用起来.(/滑稽脸) 之前想学习Mysql(Windows下配置真麻烦),学会了Docker就方便了,直接使用Docker创建一个Mysql ...
- MySQL使用和操作总结(《MySQL必知必会》读书笔记)
简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...
随机推荐
- 【C#】【Thread】SpinWait
System.Threading.SpinWait 是一个轻量同步类型,可以在低级别方案中使用它来避免内核事件所需的高开销的上下文切换和内核转换. 在多核计算机上,当预计资源不会保留很长一段时间时,如 ...
- jquery easyui 1.4.1 API( CHM版)
ChangeLog Bug The combogrid has different height than other combo components. fixed. datagrid: The r ...
- 经典SQL语句大全
一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...
- 个人作业——关于K米的产品案例分析
Notice:本文所采用的K米版本为 Version:4.3.0 Release:20161014 第一部分 调研,评测 评测: 软件的bug,功能评测,黑箱测试 1.下载并使用,描述最简单直观的个人 ...
- [转]Struts2理解--动态方法和method属性及通配符_默认Action
众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...
- C# 6.0 新特性
1.C# 6.0 示例 1: 自动属性支持初始化, 字符串嵌入的新方式, 通过 Using Static 引用静态类, nameof 表达式CSharp6/Demo1.xaml.cs /* * C# ...
- 在canvas中使用html元素
让div悬浮于canvas之上 使用z-index控制层及顺序 慕课网canvas demo <div id="canvas-wrapper"> <canva ...
- linux 下载百度盘,迅雷离线文件,解压乱码文件的方法。
首先,利用bypy的自动打包功能,将百度盘里的文件自动存放至app/bypy中,这样文件就是打包形式. 再利用 axel -n 10 "下载地址",将文件下载至本地. 下载地址获取 ...
- BZOJ 4568 幸运数字
题目传送门 4568: [Scoi2016]幸运数字 Time Limit: 60 Sec Memory Limit: 256 MB Description A 国共有 n 座城市,这些城市由 n-1 ...
- DataTable的过滤需要的数据
DataView dv = datatable.DefaultView; (1) dv.RowFilter = "RowsId>3"; //此 ...