java_jdbc

该博客展示了如何使用Java的JDBC连接MySQL数据库,包括读取配置文件、注册驱动、建立连接、执行SQL查询以及处理结果集。代码中包含了PreparedStatement的使用,用于防止SQL注入,并演示了查询操作。
摘要由CSDN通过智能技术生成
JdbcConnect.java

package com.company;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
public class JdbcConnect {
    @Test
    public void connection() throws IOException {
                //通过Properties对象获取配置文件信息
        Properties properties=new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        Connection connection;
        try {
            Class.forName(properties.getProperty("driver"));//反射方法注册驱动
            //   String url="jdbc:mysql://localhost:3306/mydb";在配置文件中
            // serverTimezone=UTC 有时参数需要加上这一句"
            connection = DriverManager.getConnection
                    (properties.getProperty("url"),properties.getProperty("user"),properties.getProperty("password"));
            System.out.println(connection);//测试连接
            //创建sql语句 update insert delete返回影响的行数 用statement.execute
//            String sql="insert into user values(null,?,?,?,?,?)";
            String sql1="select *from user where name=? and pwd=?";
            //创建执行这对象Preparedstatement
            PreparedStatement preparedStatement=connection.prepareStatement(sql1);
//            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //给参数赋值
            preparedStatement.setString(1,"小菜鸡");
            preparedStatement.setString(2,"123");
//            preparedStatement.setString(1,"小菜鸡");
//            preparedStatement.setString(2,"123");
//            preparedStatement.setString(3,"男");
//            preparedStatement.setString(4,"1234654");
//            preparedStatement.setString(5,"北京");
            //执行sql语句
//            System.out.println(preparedStatement.executeUpdate());
            //执行sql1语句,返回resultset对象 用statement.executeQuery
            ResultSet rs=preparedStatement.executeQuery();
//            int row =preparedStatement.executeUpdate();
//            System.out.println(row);//测试
            //使用while循环取出数据
            while(rs.next()){//光标下移,有数据就进入循环体
               String name=rs.getString("name");
                String pwd = rs.getString("pwd");
                System.out.println("name "+name+"pwd "+pwd);//测试
            }
            //关闭连接,后连的先关
                rs.close();
            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

mysql.properties

user=root
password=root
url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver=com.mysql.jdbc.Driver

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值