JDBC 操作mysql,实现增、删、改、查

步骤:
一、导驱动包包mysql-connector-java-8.0.11.jar
二、定义四大参数driverClassName、url、user、password
三、加载驱动类 Class.forName(driverClassName);
四、得到connection
Connection connection = DriverManager.getConnection(url, user, password);
五、得到Statement
Statement statement = connection.createStatement();
六、通过Statement执行executeUpdate方法进行增、删、改操作,执行executeQuery方法进行查询
七、executeQuery查询返回结果集resultSet进行解析取值等
八、倒关

package test2;

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

import org.junit.Test;

public class TestJDBC {
    @Test
    public void test() throws ClassNotFoundException, SQLException{
        //四大参数
        String driverClassName="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/mydb3?useSSL=false&serverTimezone=GMT-8";
        String user="root";
        String password="123";

        //加载驱动类
        Class.forName(driverClassName);

        //得到connection
        Connection connection = DriverManager.getConnection(url, user, password);

        //连接成功,执行增、删、改操作

        //得到Statement
        Statement statement = connection.createStatement();
        //通过statement执行executeUpdate方法
        String sql="INSERT INTO test VALUES (58,5.5,6,'ZHANS','DEFSSA','SADFDSAFSDAFSDA',"
                + "'DSAFDSAFDSAFDSAF','2018-6-25','14:00:00','2018-6-25 14:00:00',55)";
        sql="UPDATE test SET d='bbs',b=9.99 WHERE tid=58";
        sql="DELETE FROM test WHERE tid=55";
        int sum=statement.executeUpdate(sql);
        System.out.println(sum);
        statement.close();
        connection.close();

    }
    @Test
    public void testQurity() throws ClassNotFoundException, SQLException{
        //四大参数
        String driverClassName="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/exam?useSSL=false&serverTimezone=GMT-8";
        String user="root";
        String password="123";

        //加载驱动类
        Class.forName(driverClassName);

        //得到connection
        Connection con = DriverManager.getConnection(url, user, password);

        //得到statement
        Statement sta = con.createStatement();
        //执行executeQuery方法查询得到resultSet结果集
        String sql="SELECT * FROM emp";
        ResultSet rs=sta.executeQuery(sql);
        //解析resultSet,有一个行光标的,next,移动行光标,没有行了返回false,rs.getXxx()取值
        while(rs.next()){
            int no=rs.getInt(1);
            String name=rs.getString("ename");
            String job= rs.getString("job");
            System.out.println(no+", "+name+", "+job);
        }

        //用完了要关闭的
        rs.close();
        sta.close();
        con.close();
    }

}

以上编写不规范,规范化编程如下

package test3;

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

import org.junit.Test;

public class TestJDBC {
    @Test
    public void test() {
        // 四大参数
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/mydb3?useSSL=false&serverTimezone=GMT-8";
        String user = "root";
        String password = "123";
        Connection connection = null;
        Statement statement = null;
        try {
            // 加载驱动类
            Class.forName(driverClassName);
            // 得到connection
            connection = DriverManager.getConnection(url, user, password);
            // 连接成功,执行增、删、改操作
            // 得到Statement
            statement = connection.createStatement();
            // 通过statement执行executeUpdate方法
            String sql = "INSERT INTO test VALUES (58,5.5,6,'ZHANS','DEFSSA','SADFDSAFSDAFSDA',"
                    + "'DSAFDSAFDSAFDSAF','2018-6-25','14:00:00','2018-6-25 14:00:00',55)";
            sql = "UPDATE test SET d='bbs',b=9.99 WHERE tid=58";
            sql = "DELETE FROM test WHERE tid=55";
            int sum = statement.executeUpdate(sql);
            System.out.println(sum);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    @Test
    public void testQurity() {
        // 四大参数
        String driverClassName = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/exam?useSSL=false&serverTimezone=GMT-8";
        String user = "root";
        String password = "123";
        Connection con = null;
        Statement sta = null;
        ResultSet rs = null;
        try {
            // 加载驱动类
            Class.forName(driverClassName);
            // 得到connection
            con = DriverManager.getConnection(url, user, password);
            // 得到statement
            sta = con.createStatement();
            // 执行executeQuery方法查询得到resultSet结果集
            String sql = "SELECT * FROM emp";
            rs = sta.executeQuery(sql);
            // 解析resultSet,有一个行光标的,next,移动行光标,没有行了返回false,rs.getXxx()取值
            while (rs.next()) {
                int no = rs.getInt(1);
                String name = rs.getString("ename");
                String job = rs.getString("job");
                System.out.println(no + ", " + name + ", " + job);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 用完了要关闭的
            try {
                if (rs != null)
                    rs.close();
                if (sta != null)
                    sta.close();
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值