java之操作mysql常用方法
一般引用mysql-connector-java这个包。
package DBManager; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set; import MyClient.ArrivalCities;
import MyClient.LineInfos;
import MyClient.TrainLineInfo; public class DBHelper { private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/traininfo";
private static final String USERNAME = "root";
private static final String PASSWORD = "123abcd";
Connection conn = null;
ResultSet result = null;
PreparedStatement statement = null;
PreparedStatement stateInsert = null;
PreparedStatement stateUnlock = null; public Connection getConnection() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
} public ResultSet executeQuery(String sql) {
conn = this.getConnection();
try {
statement = conn.prepareStatement(sql);
result = statement.executeQuery();
return result;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("query data failed");
}
return null;
} public ResultSet findTrainLinesByDate(String start_city, String date) {
String dateStr = dateToString(date);
ResultSet result = this.executeQuery("select name,start_time from "
+ "line_" + dateStr + "_info " + "where start_city='"
+ start_city + "' and date='" + date + "'");
return result;
} public ResultSet findArrivalCities(String city, String line) {
ResultSet result = this
.executeQuery("select distinct arrival_city from " + city
+ "_city_info where line_num='" + line + "'");
return result;
} public ArrivalCities getAllArrivalCities(String startCity, String date) {
ArrivalCities cities = new ArrivalCities();
Set<String> list = new HashSet<String>();
ResultSet lines = this.findTrainLinesByDate(startCity, date);
try {
while (lines.next()) {
ResultSet arrivals = this.findArrivalCities(startCity,
lines.getString("name"));
while (arrivals.next()) {
list.add(arrivals.getString("arrival_city"));
}
}
if (list.size() > 0) {
cities.setCities(list);
return cities;
}
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} public LineInfos getAllLineInfos(String startCity, String arrivalCity,
String date) {
LineInfos lineinfos = new LineInfos();
ResultSet lines = this.findTrainLinesByDate(startCity, date);
if (lines != null) {
try {
while (lines.next()) {
String name = lines.getString("name");
ResultSet result = this.executeQuery("select price from "
+ startCity + "_city_info where arrival_city="
+ '"' + arrivalCity + '"' + " and line_num=" + '"'
+ name + '"');
if (result.next()) {
TrainLineInfo train = new TrainLineInfo(name,
result.getInt("price"),
lines.getLong("start_time"));
lineinfos.getList().add(train);
}
}
return lineinfos;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
} public ResultSet findNumOfSellSeats(String date, String line) {
String dateStr = dateToString(date);
ResultSet result = this.executeQuery("SELECT count(id) as num from "
+ line + "_" + dateStr + "_info where is_out=1");
return result;
} public String dateToString(String date) {
String[] result = date.split("-");
StringBuilder sb = new StringBuilder();
for (String ss : result) {
sb.append(ss);
}
return sb.toString();
} //购票,更新数据库信息
public void getBuyTicket(String dep_city,String arr_city, String date, String line){
String seat = getProperSeat(dep_city, line, date);
String sql = "update "; } public boolean executeDelete(String sql) {
conn = this.getConnection();
try {
statement = conn.prepareStatement(sql);
if (statement.executeUpdate() > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("delete data failed");
e.printStackTrace();
}
return false;
} public boolean executeUpdate(String sql) {
conn = this.getConnection();
try {
statement = conn.prepareStatement(sql);
if (statement.executeUpdate() > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("update data failed");
e.printStackTrace();
}
return false;
} public boolean executeInsert(String sql, String date, String line) {
conn = this.getConnection();
String lockSql = "lock tables " + line + "_" + date + "_info write";
String unlockSql = "unlock tables";
try {
stateInsert = conn.prepareStatement(lockSql);
statement = conn.prepareStatement(sql);
stateUnlock = conn.prepareStatement(unlockSql);
stateInsert.executeQuery();
if (statement.executeUpdate() > 0) {
stateUnlock.executeQuery();
return true;
}
stateUnlock.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("insert data failed");
e.printStackTrace();
}
return false;
} }
java之操作mysql常用方法的更多相关文章
- java JDBC操作MySQL数据库
一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...
- 在Myeclipse中用Java语言操作mysql数据库
package OperateMysql; import java.sql.*; public class MysqlTest { public static void main(String[] a ...
- 【Java】操作mysql数据库
package bd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; im ...
- Java -- JDBC 操作mysql数据库
1. Demo1 导包时 不要导具体的mysql包, 为了兼容性,导JDBC 中 sql的包既可以了. public class Demo1 { /** * @param args * @throws ...
- Java 操作MySql数据库
Java 项目开发中数据库操作是很重要的一个方面,对于初学者来说,MySql是比较容易熟悉的一种常见数据库,这篇文章记录了如何用Java来操作MySql数据库. 第一章 JDBC的概念 JDBC(Ja ...
- java分享第十七天-03(封装操作mysql类)
JAVA操作mysql所需jar包:mysql-connector-java.jar代码: import java.sql.*; import java.util.ArrayList; import ...
- Java使用Jdbc操作MySql数据库(一)
这个示例是Java操作MySql的基本方法. 在这个示例之前,要安装好MySql,并且配置好账户密码,创建一个logininfo数据库,在数据库中创建userinfo数据表.并且在表中添加示例数据. ...
- Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
- Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库
在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...
随机推荐
- 75th LeetCode Weekly Contest Smallest Rotation with Highest Score
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...
- 待修改 URAL 1542
#include<bits/stdc++.h> using namespace std; const int maxn = 2e5+2e4+11; const int dep = 666; ...
- day_06 再谈编码
1. 小数据池(常量池) 1.id() 通过id()查询一个变量在内存中的地址 2.is和== 1.is 判断左右两端内存地址是否一致,如果返回值是TRUE,则可以判断这两个变量值是同一对象 2.== ...
- day33 GIL锁 线程队列 线程池
1. 全局解释器锁GIL Python代码的执行由Python虚拟机(也叫解释器主循环)来控制.Python在设计之初就考虑到要在主循环中,同时只有一个线程在执行.虽然 Python 解释器中可 ...
- OpenCV属性页配置问题~
详细的OpenCV属性页的安装流程,参考这个网站:http://m.bubuko.com/infodetail-793518.html 运行例子时候,如果遇到这个问题,可能配置环境时候出现问题了. 此 ...
- JAVA生成word的几种方法对比
首先介绍几种java导出word方案 1.Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现 ...
- vue proxyTable 接口跨域请求调试(五)
在不同域之间访问是比较常见,在本地调试访问远程服务器....这就是有域问题. VUE解决通过proxyTable: 在 config/index.js 配置文件中 dev: { env: requir ...
- SQL事务的四种隔离级别
1未提交读(Read uncommitted):完全不锁表,所以会出现脏数据.2提交读(Read committed):1.事务1中update才锁表,可以select到最新数据. 事务2select ...
- css3制作梯形导航
/*HTML*/<div class="nav"> <a href="javascript:;">首页</a> <a ...
- Django重新整理
1.母版的继承 #base<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset ...