JDBC的一些基本应用

JDBC主要作用

JDBC的主要作用是对数据库里的数据的处理
我们先根据步骤连接数据库

1.加载MySql的驱动

Class.forName("com.mysql.jdbc.Driver");
Connection conn= DriverManager.getConnection("jdbc:mysql://:3306/test?user=root&password=123");

2.获取数据库的对象

Connection conn= DriverManager.getConnection("jdbc:mysql://:3306/test?user=root&password=123");

3.调用SQL语句

String sql="update student set age=18 where id=1";

4.获得SQL语句

Statement stat=conn.createStatement();

5.执行SQL语句

int count=stat.executeUpdate(sql);

6.根据返回值处理结果

System.out.println(count);

7.释放资源

stat.close();
conn.close();

改造后代码

private static void Test2() {
        Statement stat=null;
        Connection conn=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://:3306/test?user=root&password=123");
            String sql="select password from account where username='zhangsan'";
            stat=conn.createStatement();

            rs=stat.executeQuery(sql);
            while(rs.next()){
                String password=rs.getString("password");
                System.out.println(password);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                if(stat!=null)
                    stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try{
                if(conn!=null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
private static void Test2() {
        Statement stat=null;
        Connection conn=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://:3306/test?user=root&password=123");
            String sql="select password from account where username='zhangsan'";
            stat=conn.createStatement();

            rs=stat.executeQuery(sql);
            while(rs.next()){
                String password=rs.getString("password");
                System.out.println(password);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try{
                if(stat!=null)
                    stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try{
                if(conn!=null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

改造后的代码增加了异常处理和代码的完整性,更加完善,大家可以学着改造。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值