JavaWeb中的JDBC

需要jar包的支持
java.sql
javax.sql
mysql-con 连接驱动

实验环境搭建:

CREATE TABLE users( id INT PRIMARY KEY, `name` VARCHAR(40), 
`password` VARCHAR(40), email VARCHAR(200), birthday DATE );

INSERT INTO users(id,`name`,`password`,email,birthday) VALUES
(1,'张三','12345','[email protected]','2010-01-01')
INSERT INTO users(id,`name`,`password`,email,birthday) VALUES
(2,'李四','12345','[email protected]','2010-01-01')
INSERT INTO users(id,`name`,`password`,email,birthday) VALUES
(3,'王五','12345','[email protected]','2010-01-01')

SELECT * FROM users

导入数据库依赖:

<!--    mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>

IDEA中连接数据库

JDBC固定步骤
1.加载驱动
2.连接数据库,代表数据库
3.向数据库发送SQL的对象 statement:CRUD
4.编写SQL(根据业务,不同的sql语句)
5.执行sql
6.关闭连接

public class TestJdbc {
    
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    
    
        //配置信息
        //解决中文乱码  useUnicode=true&characterEncoding=utf-8  安全连接useSSL=true
        String url ="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String name ="root";
        String password ="123456";

        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库,代表数据库
        Connection connection = DriverManager.getConnection(url, name, password);
        //3.向数据库发送SQL的对象   statement
        Statement statement = connection.createStatement();

        //4.编写SQL
        String sql="select * from jdbc.users";

        //5.执行查询sql,返回一个ResultSet   结果集
        ResultSet rs = statement.executeQuery(sql);

        while (rs.next()){
    
    
            System.out.println("id="+rs.getObject("id"));
            System.out.println("name="+rs.getObject("name"));
            System.out.println("password="+rs.getObject("password"));
            System.out.println("email="+rs.getObject("email"));
            System.out.println("birthday="+rs.getObject("birthday"));

        }

        //6.关闭连接,释放资源,先开后关
        rs.close();
        statement.close();
        connection.close();

    }

}

预编译SQL:

public class TestJdbc2 {
    
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
    
    
        //配置信息
        //解决中文乱码  useUnicode=true&characterEncoding=utf-8  安全连接useSSL=true
        String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String name = "root";
        String password = "123456";

        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库,代表数据库
        Connection connection = DriverManager.getConnection(url, name, password);
        //3.编写sql
        String sql = "insert into jdbc.users(id, name, password, email, birthday) VALUES (?,?,?,?,?)";
        //4.预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,5);
        preparedStatement.setString(2,"戴");
        preparedStatement.setString(3,"12333");
        preparedStatement.setString(4,"[email protected]");
        preparedStatement.setDate(5,new Date(new java.util.Date().getTime()));
        //5.执行sql
        int i = preparedStatement.executeUpdate();
        if (i>0){
    
    
            System.out.println("插入成功");
        }
        //6.关闭连接,释放资源,先开后关
        preparedStatement.close();
        connection.close();

    }
}

事务:
要么都成功,要么都失败
ACID原则:保证数据的安全
1.开启事务
2.事务提交 commit()
3.事务回滚 roolback()
4.关闭事务

Junit单元测试:
依赖

<!--单元测试    -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

简单使用
@Test注解只有在方法上有效,只要加了这个注解的方法就可以直接运行

 @Test
        public void test(){
    
    
            System.out.println("Hello");
        }

猜你喜欢

转载自blog.csdn.net/weixin_47620760/article/details/114342805