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 ...
随机推荐
- HTML 学习笔记 JavaScript (prototype)
原博地址:http://www.cnblogs.com/dolphinX/p/3286177.html 原博客的作者是一个非常牛逼的前端大神,我作为一个初学者,在此借助大神的博客进行自己的学习.在这里 ...
- WPScan用法
kali下集成的WPScan用法 1.刺探基础信息:wpscan --url http://www.example.com 2.猜解后台用户名wpscan --url http://www.examp ...
- Linux Linux程序练习十九
题目:编写一个同步服务器模型 要求: )客户端A主机给服务器B主机发送报文, )B服务器主机收到报文以后同时分发给C1主机.C2主机: )C1主机和C2主机打印出客户端A的报文 bug总结:本来这道题 ...
- google-analytics的使用: 解析页面引入代码
代码整理和注释 // 创建ga()方法, 加载analytics.js文件 // a, m 作为形参,确保下面的执行不会修改外部的同名变量 (function(win, doc, o, g, ga, ...
- 分布式服务框架 dubbo/dubbox 入门示例
dubbo是一个分布式的服务架构,可直接用于生产环境作为SOA服务框架. 官网首页:http://dubbo.io/ ,官方用户指南 http://dubbo.io/User+Guide-zh.htm ...
- Zencart视频教程 Zencart模板制作教程视频 Zencart仿站教程资料
Zen Cart是国外一个免费的.界面友好,开放式源码的购物车软件,是目前外贸行业使用最为广泛的网站程序.本仿站技术需要你有一定的html和css基础,易学易懂,一步一步地教你操作和使用Zen Car ...
- jQuery取得select选择的文本与值
jquery获取select选择的文本与值获取select :获取select 选中的 text :$("#ddlregtype").find("option:selec ...
- 使用属性动画 — Property Animation
属性动画,就是通过控制对象中的属性值产生的动画.属性动画是目前最高级的2D动画系统. 在API Level 11中添加.Property Animation号称能控制一切对象的动画,包括可见的和不可见 ...
- Android四大组件之—— 使用服务进行后台操作
什么是服务 服务是一个没有可视化界面的组件,它可以在后台长期运行并进行各种操作. 服务的创建 我们只需要继承Service类并实现相应的方法即可创建服务 要想启动服务,还得在AndroidManife ...
- java集合学生管理系统
//student.java package com.sran.www; import java.util.Arrays; import java.util.Scanner; public class ...