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

jdbc (Java DataBase Connectivity,java语言连接数据库)

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

jdbc (Java DataBase Connectivity,java语言连接数据库)

1,导包,注册驱动,Class.forName();

Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jc.jdbc.Driver");

2,加载连接数据库对象

Connectin conn=DriverMansger.getConnection(url="",user="root",passward="mysql密码")

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/my2202","root", "123456");

3.准备SQL语句静态SQL语句 

 String str="INSERT INTO per (NAME ,salary)VALUES ('李丽',25000)";

 准备sql语句预编译的SQL语句(?表示占位符);

 String str="INSERT INTO per (NAME ,salary)VALUES (?,?)";

4,加载masql执行对象;

1) Statement sta= conn.createStatement();Statement是执行对象

2)PreparedStatement prep = conn.prepareStatement();prepareStatement是预编译对象

5,执行sql语句 ;

1)ResultSet set1 = sta.executeQuery();查询的sql语句 返回一个集合ResultSet

2)int i = sta.executeUpdate();静态的sql语句,返回一个值

6.返回结果

7,关闭资源,

1)stat.close();conn.close();(executeUpdate()时)

2)set.close()stat.close();conn.close(); (executeQuery()

jdbc工具类(自定义)

package com.day5.jdbc;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DruidConnection {
                 
    private static ThreadLocal thread =new ThreadLocal<>();
    private static DataSource dataSource ;
    static{
        try {
        InputStream stream = DruidConnection.class.getClassLoader().getResourceAsStream("druid.properties");
            System.out.println(stream);
        Properties prop = new Properties();
        prop.load(stream);
        dataSource = DruidDataSourceFactory.createDataSource(prop);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private DruidConnection(){}
    public static Connection getConnection() {
        Connection connection = thread.get();
        if (connection == null) {
            try {
//                通过getConnection获取connection
                 connection = dataSource.getConnection();
//                 将connection放入线程集合中
                thread.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
        return null;
    }
    public static DataSource getDataSource(){
        return dataSource;
    }
    public static void getClose(Connection connection, ResultSet resultSet, PreparedStatement stat){
    if (connection!=null){
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (resultSet!=null){
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (stat!=null){
        try {
            stat.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

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

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

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