JDBC连接MySQL 、JDBC连接Oracle (跳转

JDBC连接MySQL

import org.junit.Test;

import java.sql.*;

/**
* JDBC连接MySQL
*/
public class JDBCConnMysql {
/**
* 连接驱动
*/
public static final String DRIVER = "com.mysql.jdbc.Driver"; /**
* 连接数据库URL
* 旧版本5.7以下: jdbc:mysql://127.0.0.1:3306/<database>?useUnicode=true&characterEnocding=utf-8
* 5.7-6版本: jdbc:mysql://127.0.0.1:3306/<database>?useUnicode=true&characterEnocding=utf-8&useSSL=false
* 6版本以上:jdbc:mysql://127.0.0.1:3306/<database>?useUnicode=true&characterEnocding=utf-8&useSSL=false&serverTimezone=UTC
*/
public static final String CONNECT_URL = "jdbc:mysql://<host>:<port>/<database>?useUnicode=true&characterEnocding=utf-8&useSSL=false"; /**
* 连接用户名
*/
public static final String USERNAME = "<username>"; /**
* 连接密码
*/
public static final String PASSWORD = "<password>"; /**
* 查询sql语句
*/
private static String SQL = null; /**
* 创建一个数据库连接
*/
Connection connection = null; /**
* 创建预编译语句对象
*/
PreparedStatement preparedStatement = null; CallableStatement callableStatement = null; /**
* 创建一个结果集对象
*/
ResultSet resultSet = null; /**
* 查询数据
*/
@Test
public void queryAll() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
SQL = "select * from <table>";
preparedStatement = connection.prepareStatement(SQL);
// 4.遍历数据
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt("id") + "\t" + resultSet.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 增加数据
*/
@Test
public void add() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
SQL = "insert into <table>(<column1>,<column2>) values(?,?)";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setInt(1, <xxx>);
preparedStatement.setString(2, "<xxx>");
// 4.增加数据
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 删除数据
*/
@Test
public void del() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
SQL = "delete from <table> where <row> = ?";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setInt(1, <xxx>);
// 4.删除数据
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 更改数据
*/
@Test
public void update() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
preparedStatement = connection.prepareStatement("update <table> set <column> = ? where <row> = ?");
preparedStatement.setString(1, "<xxx>");
preparedStatement.setInt(2, <xxx>);
// 4.更改数据
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 执行存储过程
*/
@Test
public void executeProcedure() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
SQL = "{call <procedure>(?,?)}";
callableStatement = connection.prepareCall(SQL);
callableStatement.setInt(1, <xxx>);// 输入参数
callableStatement.registerOutParameter(2, JDBCType.FLOAT);// 输出参数
// 4.执行存储过程
boolean flag = callableStatement.execute();// 是否有结果集
while (flag) {
System.out.println("总计:" + callableStatement.getFloat(2));// 取得输出参数
resultSet = callableStatement.getResultSet();// 取得查询结果集
while (resultSet.next()) {
System.out.println(resultSet.getInt("<columnlabel>") + "\t" + resultSet.getFloat("<columnlabel>"));
}
flag = callableStatement.getMoreResults();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 执行函数
*/
@Test
public void executeFunction() {
try {
// 1.加载驱动进入内存
Class.forName(DRIVER);
// 2.连接数据库
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
// 3.执行sql语句:可以有效防止两次执行同一sql,提高性能
SQL = "{?=call <function>(?)}";
callableStatement = connection.prepareCall(SQL);
callableStatement.registerOutParameter(1, JDBCType.FLOAT);// 输出参数
callableStatement.setInt(2, <xxx>);// 输入参数
// 4.执行函数
callableStatement.execute();
System.out.println("总计:" + callableStatement.getFloat(1));// 取得输出参数
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5.关闭
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

JDBC连接Oracle

package com.qushihan.JDBC;

import oracle.jdbc.OracleTypes;
import org.junit.Test; import java.sql.*; /**
* JDBC连接Oracle
*/
public class JDBCConnOracle {
/**
* 连接驱动
*/
private static final String DRIVER = "oracle.jdbc.OracleDriver"; /**
* 连接数据库URL
*/
private static final String CONNECT_URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; /**
* 连接用户名
*/
private static final String USERNAME = "scott"; /**
* 连接密码
*/
private static final String PASSWORD = "tiger"; /**
* 查询sql语句
*/
private static String SQL = null; /**
* 创建一个数据库连接
*/
Connection connection = null; /**
* 创建预编译语句对象
*/
PreparedStatement preparedStatement = null; CallableStatement callableStatement = null; /**
* 创建一个结果集对象
*/
ResultSet resultSet = null; /**
* 增加数据
*/
@Test
public void add() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "INSERT INTO student(id, name, age) VALUES (?,?,?)";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setInt(1, 4);
preparedStatement.setString(2, "rose");
preparedStatement.setInt(3, 21);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 删除数据
*/
@Test
public void del() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "DELETE student where NAME = ?";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setString(1, "rose");
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 修改数据
*/
@Test
public void update() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "UPDATE student SET age = ? WHERE id = ?";
preparedStatement = connection.prepareStatement(SQL);
preparedStatement.setInt(1, 22);
preparedStatement.setInt(2, 4);
preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 查询数据
*/
@Test
public void queryAll() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "SELECT * FROM student";
preparedStatement = connection.prepareStatement(SQL);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt("id") + "\t" + resultSet.getString("name") + "\t" + resultSet.getInt("age"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 准备要执行的存储过程
* CREATE OR REPLACE PROCEDURE GET_AGE_BY_NAME(
* V_NAME IN STUDENT.NAME%TYPE,
* V_AGE OUT STUDENT.AGE%TYPE
* )
* IS
* BEGIN
* SELECT AGE INTO V_AGE
* FROM STUDENT
* WHERE NAME = V_NAME;
* END;
*/
@Test
public void executeProcedure() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "{CALL GET_AGE_BY_NAME(?, ?)}";
callableStatement = connection.prepareCall(SQL);
callableStatement.setString(1,"mike");
callableStatement.registerOutParameter(2, OracleTypes.NUMBER);
callableStatement.execute();
int age = callableStatement.getInt(2);
System.out.println("mike年龄为:" + age + "岁");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
} /**
* 准备执行的存储函数
* CREATE OR REPLACE FUNCTION COMPUTE_AVERAGE_AGE
* RETURN NUMBER
* AS
* V_SUM NUMBER := 0;
* V_COUNT BINARY_INTEGER := 0;
* CURSOR C IS
* SELECT * FROM STUDENT;
* BEGIN
* FOR V_STUDENT IN C LOOP
* V_SUM := V_SUM+V_STUDENT.AGE;
* V_COUNT := V_COUNT + 1;
* END LOOP;
* RETURN V_SUM/V_COUNT;
* END;
*/
@Test
public void executeFunction() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(CONNECT_URL, USERNAME, PASSWORD);
SQL = "{?=call COMPUTE_AVERAGE_AGE()}";
callableStatement = connection.prepareCall(SQL);
callableStatement.registerOutParameter(1, OracleTypes.DOUBLE);
callableStatement.execute();
double average_age = callableStatement.getDouble(1);
System.out.println("平均年龄为:" + average_age + "岁");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (callableStatement != null) {
try {
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}

JDBC连接MySQL与Oracle的更多相关文章

  1. JDBC连接MySQL、Oracle和SQL server的配置

    什么是JDBC 我们可以将JDBC看作是一组用于用JAVA操作数据库的API,通过这个API接口,可以连接到数据库,并且使用结构化查询语言(SQL)完成对数据库的查找,更新等操作. JDBC连接的流程 ...

  2. JDBC连接各种数据库的方法,连接MySql,Oracle数据库

    JDBC连接各种数据库的方法: JDBC编程步骤: 1.导入jar包 2.注册驱动 3.获取数据库连接对象 4.定义SQL语句 5.获得执行SQL语句对象statemnet 6.执行SQL语句 7.处 ...

  3. JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库

    首先是JDBC 连接Oracle  数据库 package com.util; import com.pojo.UserInfo; import java.sql.*; public class DB ...

  4. JDBC连接MySQL 方法 实例及资料收集

    JDBC连接MySQL 方法 实例及资料收集 准备工作 首先,安装MySQL,配置用户名和密码,创建数据库. 可参见之前的文章: http://www.cnblogs.com/mengdd/p/315 ...

  5. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  6. JDBC连接MySQL数据库及示例

      JDBC是Sun公司制定的一个可以用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一 ...

  7. Jsp 连接 mySQL、Oracle 数据库备忘(Windows平台)

    Jsp 环境目前最流行的是 Tomcat5.0.Tomcat5.0 自己包含一个 Web 服务器,如果是测试,就没必要把 Tomcat 与 IIS 或 Apache 集成起来.在 Tomcat 自带的 ...

  8. 修改sqlarchemy源码使其支持jdbc连接mysql

    注意:本文不会将所有完整源码贴出,只是将具体的思路以及部分源码贴出,需要感兴趣的读者自己实验然后实现吆. 缘起 公司最近的项目需要将之前的部分业务的数据库连接方式改为jdbc,但由于之前的项目都使用s ...

  9. Java编程学习之JDBC连接MySQL

    JDBC连接MySQL 一.对JDBC连接数据库的步骤1.加载数据库驱动//加载驱动Class.forName(driverClass)-------------------------------- ...

随机推荐

  1. coon's patch

    作者:桂. 时间:2018-05-23  06:11:54 链接:https://www.cnblogs.com/xingshansi/p/9070761.html 前言 早晨突然想到计算机模型的各种 ...

  2. 在WPF中的Canvas上实现控件的拖动、缩放

    如题,项目中需要实现使用鼠标拖动.缩放一个矩形框,WPF中没有现成的,那就自己造一个轮子:) 造轮子前先看看Windows自带的画图工具中是怎样做的,如下图: 在被拖动的矩形框四周有9个小框,可以从不 ...

  3. Git秘钥生成以及Gitlab配置(附以下问题解决方法:Key is invalid Fingerprint cannot be generated)

    在进行Git密钥配置时,总是提示: “The form contains the following errors:Key is invalidFingerprint cannot be genera ...

  4. hive hbase区别

    1.hive是sql语言,通过数据库的方式来操作hdfs文件系统,为了简化编程,底层计算方式为mapreduce. 2.hive是面向行存储的数据库. 3.Hive本身不存储和计算数据,它完全依赖于H ...

  5. idea 下 encodings.xml 的正确位置

    在多个module存在的情况下 encodings.xml在 project 下的.idea 下面         这个就是最父级project

  6. 【iCore4 双核心板_ARM】例程三十:U_DISK_IAP_FPGA实验——更新升级FPGA

    实验现象及操作说明: 1.将升级文件拷入U盘system文件夹中,通过U盘转接线将U盘连接到iCore4 USB OTG接口. 2.烧写程序成功,绿色ARM·LED灯点亮,三色FPGA·LED灯循环点 ...

  7. spring-boot-actuator报错Full authentication is required to access this resource

    解决办法[设置端点访问 ]: 1, 关闭验证 management.security.enabled=false 2,开启HTTP basic认证 -  添加依赖 <dependency> ...

  8. CustomDrawableTextView

    public class CustomDrawableTextView extends TextView{ //image width.height private int imageWidth; p ...

  9. 【QT】二进制读取图像文件测试

    QDataStream in(&file); int n; in >> n ; file.close(); qDebug() << n<<"en& ...

  10. 如何用AJax提交name[]数组?

    https://www.cnblogs.com/junzilan/p/5424120.html