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 ...
随机推荐
- dedecms 的采集
http://www.360doc.com/content/14/0521/09/13870710_379547377.shtml http://www.360doc.com/content/14/0 ...
- InterruptionInJava
package com.test; public class InterruptionInJava implements Runnable{ public static void main(Strin ...
- python3 + pycharm+requests+HTMLTestRunner生成不了测试报告html
生成不了测试文件,是运行的方式不对.因为在运行的时候,pycharm默认使用unit-test运行,所以没有生成测试报告.至于为什么会这样子,我就不清楚了,不过想了解更多的朋友,可以百度一下. 解决的 ...
- netty在rpc MQ中的应用
https://files.cnblogs.com/files/yszzu/netty-rpc-parent.zip https://github.com/apache/rocketmq/blob/m ...
- MySQL 0 学习
ubuntu 安装mysql 创建用户 以及外部如何可视化连接的 方法 https://www.linuxidc.com/Linux/2017-01/139502.htm 2.3 MyS ...
- VS 2010 快捷键大全
Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O 打开文件 CTRL ...
- Murano Weekly Meeting 2015.09.15
Meeting time: 2015.September.15th 1:00~2:00 Chairperson: Serg Melikyan, PTL from Mirantis Meeting s ...
- Murano Weekly Meeting 2015.09.01
Meeting time: 2015.September.1st 1:00~2:00 Chairperson: Nikolay Starodubtsev, from Mirantis Meeting ...
- Ubuntu环境下安装Bochs
首先说一下我的Ubuntu版本,敲命令 sudo lsb_release -a 就可以看到 No LSB modules are available. Distributor ID: Ubuntu D ...
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
最近在本地搭建的LNMP的开发环境.为了开发的时候不影响前端的正常开发就屏蔽的PHP里面php.ini中的一些错误提示.但是这样一来,就影响到了后端开发的一些问题比如不能及时调试开发中的一些问题 ng ...