栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 使用JDBC连接数据库步骤及示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 使用JDBC连接数据库步骤及示例

一、工具类:其中dataUrl、userName、passpord分别为数据库的连接串、用户名、密码。

package com.test.utils;

import com.neusoft.ehrss.base.si.person.documentPlat.config.documentConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;


@Component
public class JDBCUtil {

    @Autowired
    private documentConfiguration documentConfiguration;

    
    public Connection getConnection() {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            conn = DriverManager.getConnection(documentConfiguration.getDataUrl(), documentConfiguration.getUsername(), documentConfiguration.getPassword());
            //是否自动提交
            conn.setAutoCommit(false);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("==数据库连接失败:" + e.getMessage());
        }
        return conn;
    }

    
    public void release(Connection conn, PreparedStatement pstmt) {
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

二、代码示例

1、创建数据库连接:

  //获取数据库连接
  Connection conn = documentServiceImpl.jDBCUtil.getConnection();

2、创建一个Statement

//创建Statement
 PreparedStatement pstmt = null;

要执行SQL语句,必须获得java.sql.Statement实例,分以下三种类型:

  • 执行静态的SQL语句,通常通过Statement实例实现。
  • 执行动态的SQL语句,通常通过PreparedStatement实例实现。
  • 执行数据库存储过程,通常通过CallableStatement实例实现。

 对应的实现方式如下:

  1. Statement stmt = con.createStatement() ; 
  2. PreparedStatement pstmt = con.prepareStatement(sql) ;
  3. CallableStatement cstmt =   con.prepareCall("{CALL demoSp(? , ?)}") ;   

3、执行sql语句

提供了三种执行SQL语句的方法:

  1. ResultSet executeQuery(String sqlString)执行查询数据库的SQL语句,返回一个结果集(ResultSet)对象
  2.  int executeUpdate(String sqlString):用于执行INSERT、UPDATE或DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等
  3. execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的语句

4、处理结果

两种情况:

  1. 执行查询返回的结果是一个ResultSet对象。
  2. 执行更新返回的是本次操作影响到的记录数。 

使用结果集对象的访问方法获取数据:

  1. while(rs.next()){   
  2.          String name = rs.getString("name") ;   
  3.     String pass = rs.getString(1) ; // 此方法比较高效   
  4.     }   

5、关闭数据库连接

  1. 关闭记录集(如果有的话)
  2. 关闭声明
  3. 关闭连接 

 代码片段:

Connection conn = documentServiceImpl.jDBCUtil.getConnection();
PreparedStatement pstmt = null;
String sql = "SELECT * FROM DUAL"
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "测试");
pstmt.setLong(2, 123L);
pstmt.setDate(3, new java.sql.Date(new java.util.Date().getTime()));
int row = pstmt.executeUpdate();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/462175.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号