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) 解压 ...
随机推荐
- 使用go写一个检测tcpudp状态的包
使用go写一个检测tcpudp状态的包 http://www.2cto.com/os/201501/367596.html
- 学习struts2
有部分内容转载牛人的博客: http://blog.csdn.net/hudie1234567/article/details/6730481 http://blog.csdn.net/lishuan ...
- UVALive - 5844
题是pdf版 Sample Input23mississippinni55i55ippi2foobar|=o08arSample Output10 /** 题意:给出一个normal串,一个leet串 ...
- Selenium2+python自动化-窗口多标签处理方法总结(转载)
本篇转自博客:上海-小T 原文地址:https://i.cnblogs.com/EditArticles.aspx?opt=1 我们在用Selenium遇到多个浏览器窗口或单个浏览器多个标签(Tab) ...
- C# axWindowsMediaPlayer制作播放器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 新电脑配置 git 同步github账户
1.下载安装git 2.初始化 仓库文件夹 git init 3.生成公钥ssh-keygen -t rsa -C "youremail@example.com"4.github ...
- eclipse中的aptana插件的安装
先下载 aptana插件包 我安装的eclipse版本是 indido版本号的. 三步骤: 1.将aptana解压到eclipse的目录下 2.打开eclipse目录下的dropins文件,新建一 ...
- (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest
layout: post title: (寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest author: "luow ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)
D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...
- 6、Django实战第6天:用户登录
今天开始,我们需要来写后台逻辑了.... 后台逻辑代码都是编写在views.py文件里面,今天要完成的登录功能,因此来编辑users.views.py 这里我们根据请求方法来判断分为2种情况,网页默认 ...