java学习笔记-jdbc篇

来源:互联网 发布:慕课网php百度网盘 编辑:程序博客网 时间:2024/05/16 15:27

一:JDBC概述
jdbc(Java DataBase Connectivity)是访问数据库接口,一种基准,具体实现需要相关的数据库公司。
二:JDBC连接数据库

  1. 导入相关数据库驱动包
  2. 加载数据库驱动到虚拟机
try {            Class.forName("com.mysql.jdbc.Driver");            System.out.println("j");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }

3.连接数据库

Connection connection=null;        try {        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/dvdsystem", "root", "root");        System.out.println("进行数据库相关操作");        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }

4.进行数据库相关操作
5.关闭数据库连接

finally{            try {                connection.close();            } catch (SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }

三.使用Statement接口实现增,删,改操作

//DbUtil为封装的获得连接DbUtil dbUtil=new DbUtil();String sqlinsert="insert into person (PER_NAME,PER_AGE)values('admin2',123)";String sqlupdate="update person set PER_NAME='admin56' where PERSON_ID=1";String sqldelete="delete from person where PERSON_ID=1";//获得连接Connection conection = dbUtil.getConection();//获得statementStatement statement=conection.createStatement();//executeUpdate可以执行insert,update,delete语句,返回影响的行数int i = statement.executeUpdate(sqldelete);//最后关闭statement.close();conection.close();

四.使用PreparedStatement接口实现增,删,改操作
PreparedStatement是Statement的子接口,可以处理预编译,一般开发使用PreparedStatement接口。

DbUtil dbUtil=new DbUtil();String sqlinsert="insert into person (PER_NAME,PER_AGE)values(?,?)";String sqlupdate="update person set PER_NAME=? where PERSON_ID=1";String sqldelete="delete from person where PERSON_ID=?";//获得连接Connection conection = dbUtil.getConection();//获得prepareStatement,预选把sql语句装载进入PreparedStatement prepareStatement = conection.prepareStatement(sqlinsert);//把占位符填充进入prepareStatement.setString(1,"aacd");prepareStatement.setInt(2,3434);//返回影响的行数int i = prepareStatement.executeUpdate();prepareStatement.close();conection.close();

五. ResultSet结果集
executeQuery()方法,用于查询语句使用,返回result结果集,最后遍历输出,一般最后封装到一个类中,最后封装成一个list结果集。

DbUtil dbUtil=new DbUtil();String sqlinquery="select * from person";//获得连接Connection conection = dbUtil.getConection();//获得prepareStatementPreparedStatement prepareStatement = conection.prepareStatement(sqlinquery);//executeQuery()方法,返回result结果集ResultSet resultSet = prepareStatement.executeQuery();//遍历数据库中某表的每一行while (resultSet.next()) {int id = resultSet.getInt("PERSON_ID");//参数为列名System.out.println(id);String personname = resultSet.getString("PER_NAME");System.out.println(personname);int personage = resultSet.getInt("PER_AGE");System.out.println(personage);        }prepareStatement.close();conection.close();

六.处理大数据对象(CLOB数据和BLOB数据)
CLOB数据:存储大数据字符对象,比如长篇小说;
BLOB数据:存储二进制大数据,比如一些音频文件;
1.CLOB插入
数据库中字段类型为longtext,文本的编码需统一为utf-8

//把文本插入数据库中File contextFile=new File("C:\\Users\\q\\Desktop\\DVD\\text.txt");InputStream inputStream = new FileInputStream(contextFile);prepareStatement.setAsciiStream(3, inputStream,contextFile.length());

2.CLOB查询
在result.next中:

Clob clob = resultSet.getClob("context");String context = clob.getSubString(1,(int)clob.length());

3.BLOB插入

File contextFile=new File("C:\\Users\\q\\Desktop\\DVD\\2.png");InputStream inputStream = new FileInputStream(contextFile);prepareStatement.setBinaryStream(2, inputStream, contextFile.length());

4.BLOB查询
查询出图片,并复制到桌面

            Blob blob = resultSet.getBlob("context");            InputStream in = blob.getBinaryStream();            byte[] b = new byte[1024];            try {                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:/Users/q/Desktop/data.png"));                int len;                while((len = in.read(b)) != -1){                    bos.write(b, 0, len);                    bos.flush();                }                bos.close();                in.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }

七. 使用元数据分析数据库
1.DatabaseMetaData 有关整个数据库的信息:表名、表的索引、数据库产品的名称和版本、数据库支持的操作。

    Connection  con = DriverManager.getConnection(url, user, password);       DatabaseMetaData dbMetaData = con.getMetaData();     System.out.println("数据库的版本:" + dbMetaData.getDatabaseProductVersion());         System.out.println("驱动程序的名称:" + dbMetaData.getDriverName());         System.out.println("驱动程序的版本:" + dbMetaData.getDriverVersion());   

2.ResultSet 关于某个表的信息或一个查询的结果。您必须逐行访问数据行,但是您可以任何顺序访问列.
ResultSetMetaData 有关 ResultSet 中列的名称和类型的信息。

ResultSetMetaData rsmd = rs.getMetaData();System.out.println("获得1列所在的Catalog名字 : " + rsmd.getCatalogName(1));  System.out.println("获得1列对应数据类型的类 " + rsmd.getColumnClassName(1));  System.out.println("获得该ResultSet所有列的数目 " + rsmd.getColumnCount()); 
原创粉丝点击