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. 第1章 重构,第一个案例(1):糟糕的statement函数设计

    1. 启航:影片出租,计算每一位顾客的消费金额并打印清单 1.1 场景说明: (1)影片分类规则:普通片.儿童片和新片等3类 (2)每种影片计算租金的方式. ①普通片:基本租金为2元,超过2天的部分每 ...

  2. iis

    IIS架构 1.   概述 为了提高IIS的可靠性,安全性以及可用性,与IIS5.0和以前更早的版本不同,IIS6.0提供了一个全新的IIS架构.这个架构的详细情况如下图所示:             ...

  3.  非法字符 原因 以及解决办法

    模板文件生成html文件之后会在body开头处加入一个可见的控制符 // 如果是Windows系统,修改为:$WIN = 1; $WIN = 0; ?> <!DOCTYPE html P ...

  4. MVC4 自定义错误页面(转)

    一.概述 MVC4框架自带了定义错误页,该页面位于Shared/Error,该页面能够显示系统未能捕获的异常,如何才能使用该页面: 二.使用步骤: 1.配置WebConfig文件,在System.We ...

  5. React官网学习笔记

    欢迎指导与讨论 : ) 前言 本文主要是笔者在React英文官网学习时整理的笔记.由于笔者水平有限,如有错误恳请指出 O(∩_∩)O 一 .Tutoial 篇 1 . React的组件类名的首字母必须 ...

  6. Mersenne twister 随机数算法实现 in Scheme

    这个实现基本上是从 Wiki 上的 Python 版翻译过来的,大量使用了赋值. ;; Mersenne twister algorithm from Wikipedia ;; returns a c ...

  7. 汇编语言标志位 含义 NV UP EI NG NZ AC PE CY

    缩写原意: Overflow of = OV NV [No Overflow] Direction df = DN (decrement) UP (increment) Interrupt if = ...

  8. Moto C118 基于 Osmocom-BB 和 OpenBTS 搭建小型GSM短信基站

    此文章PDF文档下载地址:点击下载 0x00 写在前面 大家应该都听说过摩托罗拉C118配合Osmocom-BB实现GSM网络下的短信拦截功能吧,在14年左右新出了一种玩法就是Osmocom-BB的s ...

  9. 【跟着子迟品 underscore】JavaScript 中如何判断两个元素是否 "相同"

    Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...

  10. MapReduce实现倒排索引(类似协同过滤)

    一.问题背景 倒排索引其实就是出现次数越多,那么权重越大,不过我国有凤巢....zf为啥不管,总局回应推广是不是广告有争议... eclipse里ctrl+t找接口或者抽象类的实现类,看看都有啥方法, ...