一般引用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常用方法的更多相关文章

  1. java JDBC操作MySQL数据库

    一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...

  2. 在Myeclipse中用Java语言操作mysql数据库

    package OperateMysql; import java.sql.*; public class MysqlTest { public static void main(String[] a ...

  3. 【Java】操作mysql数据库

    package bd; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; im ...

  4. Java -- JDBC 操作mysql数据库

    1. Demo1 导包时 不要导具体的mysql包, 为了兼容性,导JDBC 中 sql的包既可以了. public class Demo1 { /** * @param args * @throws ...

  5. Java 操作MySql数据库

    Java 项目开发中数据库操作是很重要的一个方面,对于初学者来说,MySql是比较容易熟悉的一种常见数据库,这篇文章记录了如何用Java来操作MySql数据库. 第一章 JDBC的概念 JDBC(Ja ...

  6. java分享第十七天-03(封装操作mysql类)

     JAVA操作mysql所需jar包:mysql-connector-java.jar代码: import java.sql.*; import java.util.ArrayList; import ...

  7. Java使用Jdbc操作MySql数据库(一)

    这个示例是Java操作MySql的基本方法. 在这个示例之前,要安装好MySql,并且配置好账户密码,创建一个logininfo数据库,在数据库中创建userinfo数据表.并且在表中添加示例数据. ...

  8. Java框架spring Boot学习笔记(五):Spring Boot操作MySQL数据库增、删、改、查

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

  9. Java框架spring Boot学习笔记(四):Spring Boot操作MySQL数据库

    在pom.xml添加一下代码,添加操作MySQL的依赖jar包. <dependency> <groupId>org.springframework.boot</grou ...

随机推荐

  1. 毕业设计 python opencv实现车牌识别 预处理

    主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...

  2. bash 中 trim 字符串(去除首尾空格) - grep 去空行

    在 bash 下如何去除一个字符串首尾的空格(也就是 trim)呢?其实有一个简单的办法: $ echo $STR 注 意 $STR 不要带引号.因为 $STR 展开后,会作为 echo 的参数.那么 ...

  3. pm2 启动后台 node js

    1,安装node js 参看:https://www.cnblogs.com/wf-l5201314/p/9229974.html 2,pm2安装(安装环境linux / os) 命令:npm ins ...

  4. PHP HTTP的PUT,DELETE的使用

    GET操作是安全的.所谓安全是指不管进行多少次操作,资源的状态都不会改变.比如我用GET浏览文章,不管 浏览多少次,那篇文章还在那,没有变化.当然,你可能说每浏览一次文章,文章的浏览数就加一,这不也改 ...

  5. 如何用Rational rose创建类图

    UML中各种图形概要: 图名 对照 说明 用例图 use case diagram 用例图表明系统做什么,与谁交互.用例是系统提供的功能,参与者是系统与谁交互,参与者可以是人.系统或其他实体.一个系统 ...

  6. idea各种快捷键

    工作的的时候,如果不知道idea一些方便的快捷键会大大影响工作效率,今天打算看看这些小技巧: https://blog.csdn.net/linsongbin1/article/details/802 ...

  7. java——int、args[]传参、标签、数字塔?、一个输入格式

    1.当int型整数超出自己范围时,会从它的上界重新开始. public class exp { public static void main(String[] args) { int i = 214 ...

  8. 转 sql profile 绑定 litera and move profile to another db l for spa

    SQL TYPE 1:for bind value sql , first create a good plan with literal and with good  profile. then u ...

  9. Unity Resources.Load

    GameObject bulletPrefab; void Start() {   bulletPrefab = Resources.Load("bulletPrefab")  a ...

  10. Linux cp命令拷贝 不覆盖原有的文件

    cp 参数说明: -i或--interactive  覆盖既有文件之前先询问用户. -r  递归处理,将指定目录下的文件与子目录一并处理. -R或--recursive  递归处理,将指定目录下的所有 ...