jdbc配置

导入数据库包
image
增加到librity里面
image

练习
image

连接数据库代码
JDBCUtils

package lesson.utils;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.server.ExportException;
import java.sql.*;
import java.util.Properties;

/**
 * Created by Administrator on 2022/8/1.
 */
public class JdbcUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static {

        try{
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
.			//加载驱动
            Class.forName(driver);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,username,password);
    }

    public static void release(Connection conn, Statement st, ResultSet rs){
        if(rs!=null){
            try{
                rs.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        if(st!=null){
            try{
                st.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try{
                conn.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }

    }

}

db.properties

driver=com.mysql.jdbc.Driver
#数据库要修改成自己的数据库
url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true
#自己的用户名
username=
#自己的密码
password=

TestJdbc

package lesson.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by Administrator on 2022/8/1.
 */
public class TestInsert {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            conn = JdbcUtils.getConnection();
            st = conn.createStatement();
            String sql ="INSERT  INTO student VALUES(1008,111111,'乔丹',1,1,123445,'河北石家庄','1999-12-12 00:00:00',4545545,5255);";
            int i = st.executeUpdate(sql);
            if(i>0){
                System.out.println("插入成功!");
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtils.release(conn,st,rs);
        }
    }
}

使用prepareStatement

package dao;

import util.JdbcUtils;

import java.sql.*;

/**
 * Created by Administrator on 2022/8/16.
 */
public class AddUserImpl implements AddUserDao {
    public int adduserDao(String name, String password) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        int i = 0;

        try{
            conn = JdbcUtils.getConnection();
			//每个问号对应一个数据
            String sql ="INSERT INTO USER VALUE (?,?,?)";
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
			//对应问号
            preparedStatement.setInt(1,3);
            preparedStatement.setString(2,name);
            preparedStatement.setString(3,password);


            i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("插入成功!");
            }
        }catch(SQLException e){
            e.printStackTrace();
        } finally {
            JdbcUtils.release(conn,st,rs);
        }
        return i;
    }
}

posted @   笑到肚子疼  阅读(81)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示