20170204:数据库建模、JDBC操作数据库的初步学习

13 篇文章 0 订阅
4 篇文章 1 订阅

JDBC操作数据的初步学习

JDBC的基本概念

1.JDBC(Java DataBase Connection,java数据库连接),由一些接口和类构成的API。

JAVA应用程序–>JDBC API–>JDBC驱动程序–>数据库

JDBC操作数据库的步骤

1.注册驱动(只做一次)
2.建立连接(Connection)
3.创建执行SQL的语句(Statement)
4.执行语句(通过Statement或PreparedStatement)
5.处理执行结果(ResultSet)
6.释放资源

建立连接(Connection)

Connection conn = DriverManager.getConnection(url,user,password);

url格式: jdbc:子协议://主机名:端口/数据库名?属性名=属性值&…;

user(用户名),password(密码)可以用”属性名=属性值”方式告诉数据库;

其他参数如:useUnicode=true&characterEncoding=GBK;

接下来创建一张学生表,并用jdbc来操作一下对学生表的增删改查的操作:

这里写图片描述

在用java程序操作数据库时需要用到jar包,从外部导入到工程中,主要是用到包中的驱动类,程序通过加载驱动达到与数据库的连接:

这里写图片描述

接下来就是将此jar包放于工程中,先在工程下建立一个folder,取名为lib,将此jar包拷到此文件下,再将此jar包导入到library库找那个即可

下面写一个InsertDemo:

package jdbc2;

import java.sql.*;

public class InsertDemo {

    public static void main(String[] args) {

        Connection conn=null;
        Statement sta=null;
        try {

            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","123456");

            //3.写sql语句
            String sql = "insert into student(stuname,stuage,score) values('艾瑞莉娅',25,95.5);";

            //4.创建sql语句执行对象
            sta = conn.createStatement();

            //5.执行sql
            sta.executeUpdate(sql);
            System.out.println("执行成功。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                sta.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}

从这个简单的代码中可以看出,我这个是认真执行了上面六步,这里解释一下executeUpdate方法,参数即是传一个sql语句,返回值是sql语句执行时影响的行数,增、删、改都用这个方法。这里就不加赘述了。

下面安排个查询Demo(QueryDemo):

package jdbc2;

import java.sql.*;

public class QueryDemo {

    public static void main(String[] args) {

        Connection conn=null;
        ResultSet rs=null;
        Statement sta=null;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","123456");

            sta = conn.createStatement();

            String sql = "select stuname,stuage,score from student;";

            rs = sta.executeQuery(sql); //该方法返回查询到的结果集

            //遍历该结果集
            System.out.println("学生姓名\t学生年龄\t学生分数");
            while(rs.next()){
                String stuname = rs.getString("stuname");
                int stuage = rs.getInt("stuage");
                double score = rs.getDouble("score");
                System.out.println(stuname+"\t"+stuage+"\t"+score);
            }
            System.out.println("遍历成了。。。");
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                rs.close();
                sta.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}

遍历结果如下哈:

这里写图片描述

接下来在讨论下:以上两个程序一次只能执行一条sql语句,在Statement类中有个addBatch方法可以让一个程序同时执行多条sql语句。即所谓的批处理。上代码:

package jdbc2;

import java.sql.*;

public class BatchDemo {

    public static void main(String[] args) {

        Connection conn=null;
        Statement sta= null;

        try {

            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","123456");

            sta = conn.createStatement();

            sta.addBatch("insert into student(stuname,stuage,score) values('孙尚香',26,98),('德莱厄斯',25,98.5);");
            sta.addBatch("update student set stuname='究极皮皮怪' where stuid=2");
            sta.addBatch("delete from student where stuid=3;");

            int[] batch = sta.executeBatch();
            System.out.println("一共执行了"+batch.length+"条sql语句");
            for(int count:batch){
                System.out.println("每条sql语句影响的行数为:"+count);
            }

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

    }


}

executeBatch方法返回的是一个int型数组,数组长度就是批处理的sql语句的条数,每一个数组值代表了相应的sql语句影响的行数

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值