JDBC数据库连接技术

来源:互联网 发布:土方计算软件 编辑:程序博客网 时间:2024/06/05 17:48

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());        }