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)的更多相关文章

  1. Database学习 - mysql 连接数据库 库操作

    连接数据库 语法格式: mysql -h 服务器IP -P 端口号 -u用户名 -p密码 --prompt 命令提示符 --delimiter 指定分隔符 示例: mysql -h 127.0.0.1 ...

  2. go语言入门教程百度网盘 mysql图形化操作与数据导入

    mysql图形化操作与数据导入 @author:Davie 版权所有:北京千锋互联科技有限公司 数据库存储技术 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.每个数据库都有一个 ...

  3. MySQL常用指令,java,php程序员,数据库工程师必备。程序员小冰常用资料整理

    MySQL常用指令,java,php程序员,数据库工程师必备.程序员小冰常用资料整理 MySQL常用指令(备查) 最常用的显示命令: 1.显示数据库列表. show databases; 2.显示库中 ...

  4. zabbix数据库mariadb从服务器迁移到云mysql数据库的操作

    zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...

  5. C语言对mysql数据库的操作

    原文:C语言对mysql数据库的操作 这已经是一相当老的话题.不过今天我才首次使用,把今天的一些体会写下来,也许能给一些新手带来一定的帮助,更重要的是供自己今后忘记的怎么使用而进行查阅的! 我们言归正 ...

  6. MySQL使用和操作总结

    简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...

  7. Linux下MySQL的简单操作

    Linux下MySQL的简单操作 更改mysql数据库root的密码 首次进入数据库是不用密码的: [root@localhost ~]# /usr/local/mysql/bin/mysql -ur ...

  8. MySQL基本简单操作01

    MySQL基本简单操作 学会了安装Docker,那么就将它利用起来.(/滑稽脸) 之前想学习Mysql(Windows下配置真麻烦),学会了Docker就方便了,直接使用Docker创建一个Mysql ...

  9. MySQL使用和操作总结(《MySQL必知必会》读书笔记)

    简介 MySQL是一种DBMS,即它是一种数据库软件.DBMS可分为两类:一类是基于共享文件系统的DBMS,另一类是基于客户机——服务器的DBMS.前者用于桌面用途,通常不用于高端或更关键应用. My ...

随机推荐

  1. Spring---BeanFactory

    Spring---BeanFactory   BeanFactroy是一个Spring容器,用于创建,配置,管理bean,bean之间的依赖关系也有BeanFactory负责维护: BeanFacto ...

  2. iOS 底层框架的浅析

    1.简介 IOS是由苹果公司为iPhone.iPod touch和iPad等设备开发的操作系统. 2.知识点 iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设 ...

  3. 用上CommonMark.NET,.NET平台终于有了好用的markdown引擎

    缺少好用的markdown引擎之前一直是.NET平台上的一个痛点.因为这个痛点,我们被迫痛苦地使用了pandoc--不是pandoc做的不好,而是pandoc是由Haskell开发的,只能在Windo ...

  4. 在Windows Server 2012 R2上安装SharePoint 2013 with SP1失败,提示没有.net4.5的解决办法

    现在的Server用Windows Server 2012 R2的越来越多了,在部署带Sp1的SharePoint2013的时候,走完预安装工具后,点击setup提示缺少.net4.5. 其实Wind ...

  5. cosbench 异常 FreeMarker template error: The following has evaluated to null or missing

    问题现象: 使用Cosbench 0.4.2.c4 版本测试Ceph RGW read test失败,遇到异常如下: FreeMarker template error: The following ...

  6. java 异步处理

    详情请看:http://www.cnblogs.com/yezhenhan/archive/2012/01/07/2315645.html 引入ExecutorService 类 private st ...

  7. C#-WinForm-Treeview-树状模型

    Treeview - 树状模型 利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); TreeNo ...

  8. 【codevs2495】 水叮当的舞步

    http://codevs.cn/problem/2495/ (题目链接) 题意 给出一个N*N的矩阵,其中元素有5种颜色,每次可以将左上角元素所在的连通块更换一种颜色,连通块指相邻并且颜色相同的元素 ...

  9. 创建一个自定义颜色IRgbColor

    后续文章需要用到,很简单的一个小函数 /// <summary> /// 自定义颜色 /// </summary> /// <param name="r&quo ...

  10. Node.js入门笔记(3):全局对象(2)

    buffer 用于更好操作二进制数据,他是一个全局变量.类似数组. var a=new Buffer(); buffer类的三种实现 第一种创建方式 new Buffer(size);size[Num ...