Java连接MySQL数据库,并进行增删改查
1、具体的代码实现
import java.sql.*;
public class DatabaseService {
/**
* Create Connection
*
* @param dbtype
* @param username
* @param password
* @param url
* @return
* @throws Exception
*/
public static Connection connectDBDriver(String dbtype, String username,
String password, String url) throws Exception {
Connection conn = null;
try {
if (dbtype.equals("mysql")) {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} else if (dbtype.equals("oracle")) {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
} else {
}
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* close DB
*
* @param conn
* @throws Exception
*/
public void closeDBDriver(Connection conn) throws Exception {
try {
conn.close();
} catch (Exception e) { /* ignore close errors */
e.printStackTrace();
}
}
/**
* get ResultSet
*
* @param conn
* @param sql
* @return
* @throws Exception
*/
private static ResultSet getResultSet(Connection conn, String sql)
throws Exception {
ResultSet resultSet = null;
try {
// PreparedStatement pstmt;
// ResultSet rset;
//任意的前后滚动;设置为只读类型的参数
Statement statement = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// pstmt = conn.prepareStatement(sql);
resultSet = statement.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}
/**
* get ColumnCount
*
* @param resultSet
* @return
* @throws Exception
*/
private static int getColumnCount(ResultSet resultSet) throws Exception {
int columnCount = 0;
try {
// ResultSet resultSet = this.getResultSet(conn, sql);
//getMetaData() 获取此 ResultSet 对象的列的编号、类型和属性
//getColumnCount() 返回此 ResultSet 对象中的列数。
columnCount = resultSet.getMetaData().getColumnCount();
if (columnCount == 0) {
}
} catch (Exception e) {
e.printStackTrace();
}
return columnCount;
}
/**
* get ColumnCount
*
* @param conn
* @param sql
* @return
* @throws Exception
*/
public static int getColumnCount(Connection conn, String sql) throws Exception {
int columnCount = 0;
try {
// ResultSet resultSet = this.getResultSet(conn, sql);
columnCount = getResultSet(conn, sql).getMetaData()
.getColumnCount();
if (columnCount == 0) {
}
} catch (Exception e) {
e.printStackTrace();
}
return columnCount;
}
/**
* get RowCount
*
* @param conn
* @param sql
* @return
* @throws Exception
*/
public int getRowCount(Connection conn, String sql) throws Exception {
int rowCount = 0;
try {
ResultSet resultSet = getResultSet(conn, sql);
// boolean last() 将光标移动到此 ResultSet 对象的最后一行。
resultSet.last();
// boolean last() 将光标移动到此 ResultSet 对象的最后一行。
rowCount = resultSet.getRow();
if (rowCount == 0) {
}
} catch (Exception e) {
e.printStackTrace();
}
return rowCount;
}
/**
* get RowCount
*
* @param resultSet
* @return
* @throws Exception
*/
private static int getRowCount(ResultSet resultSet) throws Exception {
int rowCount = 0;
try {
resultSet.last();
rowCount = resultSet.getRow();
if (rowCount == 0) {
}
} catch (Exception e) {
e.printStackTrace();
}
return rowCount;
}
/**
* get data by row index and col index
*
* @param conn
* @param sql
* @param row
* @param col
* @return
* @throws Exception
*/
public static String getData(Connection conn, String sql, int row, int col)
throws Exception {
String data = null;
int rownum = 0;
int rowcount = 0;
int colcount = 0;
try {
ResultSet resultSet = getResultSet(conn, sql);
colcount = getColumnCount(resultSet);
rowcount = getRowCount(resultSet);
//beforeFirst() 将光标移动到此 ResultSet 对象的开头,正好位于第一行之前。
resultSet.beforeFirst();
if (rowcount > 0) {
if (row <= 0 || row > rowcount) {
} else {
if (col <= 0 || col > colcount) {
} else {
while (resultSet.next()) {
rownum++;
if (rownum == row) {
data = resultSet.getString(col);
break;
}
}
}
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
/**
* get data by row index and col index
*
* @param conn
* @param sql
* @param row
* @param field
* @return
* @throws Exception
*/
public static String getData(Connection conn, String sql, int row, String field)
throws Exception {
String data = null;
int rownum = 0;
int rowcount = 0;
// int colcount = 0;
try {
ResultSet resultSet = getResultSet(conn, sql);
// colcount = getColumnCount(resultSet);
rowcount = getRowCount(resultSet);
resultSet.beforeFirst();
if (rowcount > 0) {
if (row <= 0 || row > rowcount) {
} else {
while (resultSet.next()) {
rownum++;
if (rownum == row) {
data = resultSet.getString(field);
break;
}
}
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
// executeUpdate方法可以执行新增、更新、删除三种sql语句
public static int executeUpdate(Connection conn, String sql) {
Statement stmt = null;
try {
stmt = conn.createStatement();
stmt.executeUpdate(sql);
int updateCount = stmt.getUpdateCount();
return updateCount;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return 0;
}
}
2、调用函数实现增删改查
public class JdbcData {
public static void main(String[] args) {
Connection connection = databaseService.connectDBDriver("mysql","username","password","URL");
String sql = "XXXX'";
//查询数据,获取到数据
String data = DatabaseService.getData(connection, sql,1,1);
String sqlstatus = "XXXX";
//update数据
DatabaseService.executeUpdate(connection,sqlstatus);
}
}
Java连接MySQL数据库,并进行增删改查的更多相关文章
- java连接mysql数据库 三 实现增删改查操作
同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...
- 【Python + Mysql】之用pymysql库连接Mysql数据库并进行增删改查操作
用pip下载pymysql并引用 具体请参考文章: <Python之MySQL数据库增删改查操作> <python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删 ...
- Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)
day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库: 简称:DataBase ---->DB 数据库即存放数据的仓库, ...
- Vc数据库编程基础MySql数据库的表增删改查数据
Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...
- Java连接Redis之redis的增删改查
一.新建一个maven工程,工程可以以jar的形式或war都行,然后导入正确的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0& ...
- mysql数据库单表增删改查命令
数据库DB-database-mysql 课程安排 第一天: 1.数据库定义以及设计 2.mysql服务端的安装 3.mysql-dos操作 库的操作 表的操作 4.mysql客户端navicate工 ...
- 48.Python中ORM模型实现mysql数据库基本的增删改查操作
首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...
- Java连接MySQL数据库及简单的增删查改操作
主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...
- .net 连接SqlServer数据库及基本增删改查
一.写在前面 因为这学期选修的 .net 课程就要上机考试了,所以总结下.net 操作 SqlServer 数据的方法.(因为本人方向是 Java,所以对.net 的了解不多,但以下所写代码均是经过测 ...
- MySQL数据库(安装+增删改查)
一. 安装 下载地址 : https://dev.mysql.com/downloads/mysql/ 1. 安装步骤 (1) 选择5.7版本 (2) 针对操作系统的不同下载不同的版本 (3) 解压 ...
随机推荐
- Codeforces #107 DIV2 ABCD
A #include <map> #include <set> #include <list> #include <cmath> #include &l ...
- python进程池pool的starmap的使用
#!/usr/bin/env python3 from functools import partial from itertools import repeat from multiprocessi ...
- [ Python - 9 ] 高阶函数map和reduce连用实例
1. 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2num( ...
- 关于ES6(ES2015)开发记坑
ES2015(以下简称ES6)在开发过程中遇到的问题: 1,必须显示声明变量 //es5中可解释为全局变量 a=5; //es6中报错:a is not defined a=5 2,对于递归调用方式必 ...
- SQLAlchemy技术文档(中文版)-上
转自:http://www.cnblogs.com/iwangzc/p/4112078.html 1.版本检查 import sqlalchemy sqlalchemy.__version__ 2.连 ...
- 在一个ros包下怎么使用另外一个自定义ros包里的message
假设自定义消息包my_message_package https://answers.ros.org/question/206257/catkin-use-ros-message-from-anoth ...
- C# 通过串口发送短信
手机短信群发作为企业日常通知,公告,天气预报等信息的一个发布平台,在于成本低,操作方便等诸多特点,成为企业通讯之首选.本文介绍短信的编码方式,AT指令以及用C#实现串口通讯的方法. 前言目前,发送短信 ...
- HDU 6319.Problem A. Ascending Rating-经典滑窗问题求最大值以及COUNT-单调队列 (2018 Multi-University Training Contest 3 1001)
2018 Multi-University Training Contest 3 6319.Problem A. Ascending Rating 题意就是给你长度为k的数列,如果数列长度k<n ...
- HDU 多校1.5
Expectation Division Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/O ...
- 模板—数学—Exgcd
模板—数学—Exgcd Code: #include <cstdio> #include <algorithm> using namespace std; int ex_gcd ...