JDBC数据库连接技术

JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力:Java DataBase Connectivity,java数据库连接。
这里写图片描述
JDBC API
供程序员调用的接口与类,集成在java.sql和javax.sql包中,如:
DriverManager类
Connection接口
Statement接口
ResultSet接口
JDBC API主要功能:与数据库建立连接、执行SQL 语句、处理结果。
下面,已MySQL数据库为例,向数据库摸个表中增加一条数据;
要建立Java与数据库之间的连接,首先,加载驱动类:

try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//创建连接和传令官对象
        Connection conn=null;
        PreparedStatement psmt=null;
        try {
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schools?useUnicode=true&characterEncoding=GBK ", "root", "123456");//其中127.0.0.1是本机的IP地址,schools是要操作的数据库的名字,root是数据库用户名,后面的是密码。
            String sql="insert into student(studentno,studentname,birthday) values(?,?,?)";
            psmt=conn.prepareStatement(sql);
            psmt.setInt(1, student.getStudentno());
            psmt.setString(2, student.getStudentname());
            psmt.setString(3, student.getBirthday());
//执行sql
            psmt.executeUpdate();
            System.out.println("ok");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
            //关闭资源
                psmt.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

数据库的增删改写法基本类似,只是
String sql=”insert into student(studentno,studentname,birthday) values(?,?,?)”;中的语句不同而已;
关于查找,除了要在数据库中找到数据,还要有接收返回数据的容器,就用到resultSet;
代码如下,一般用集合接收返回的值,然后再显示出来

public ArrayList<Student> searchStudentByStudentno(int studentno) {
        ArrayList<Student> students=new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Connection conn=null;
        PreparedStatement psaa=null;
        ResultSet rrs=null;
        try {
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/schools?useUnicode=true&amp;characterEncoding=GBK", "root", "123456");
            String sql="select studentno,studentname ,birthday from student where studentno=?";
            psaa=conn.prepareStatement(sql);
            psaa.setInt(1, studentno);
            //接收结果
            rrs=psaa.executeQuery();
            while (rrs.next()) {
                int studentno1=rrs.getInt(1);
                String studentname=rrs.getString(2);
                String birthday=rrs.getString(3);
                Student student=new Student(studentno1, studentname, birthday);
                students.add(student);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                rrs.close();
                psaa.close();
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return students;
    }
select studentno,studentname ,birthday from student where studentno=?这条语句中,where条件可以有多个,不过方法中的形参数量也要随之增加。

测试类的写法

StudentDao studao=new StudentDao();
//增加一名学生
Student student=new Student(6, "神乐", "1999-2-3");
studao.addStudent(student);
//查找
ArrayList<Student> students=studao.searchStudentByStudentno(6);
for (Student student : students) {
            System.out.println(student.getStudentno()+" "+student.getStudentname()+" "+student.getBirthday());
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值