基础回顾-JDBC操作数据库

使用JDBC完成对数据库数据的操作

1.首先创建一张数据库表

2.创建一个类,开始编写JDBC程序,这里只是插入了一条数据进行展示

package com.test.jdbc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class DemoJDBC {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		InputStream is  = null;
		OutputStream os = null;
		Reader rd  = null;
		try {
			//加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sshdemo","root","8888");
			//添加信息
			addinfo(conn,ps);
			//查询信息
			selectInfo(conn, ps, rs, is, os, rd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(is!=null){
					is.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if(os!=null){
					os.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				if(ps!=null){
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 添加数据
	 * @param conn
	 * @param ps
	 */
	private static void addinfo(Connection conn,PreparedStatement ps) {
		try {
			ps = conn.prepareStatement("insert into user (id,name,createdate,createtime,textnode,photo) values (?,?,?,?,?,?)");
			ps.setInt(1, 1);
			ps.setString(2, "测试");
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			Timestamp stamp = new Timestamp(System.currentTimeMillis());
			ps.setDate(3, date);
			ps.setTimestamp(4, stamp);
				try {
					//将文本文件内容直接输入到数据库中
					ps.setClob(5, new FileReader(new File("f:/text/a.txt")));
					//将图片文件内容直接输入到数据库中
					ps.setBlob(6, new FileInputStream("f:/image/img/1.jpg"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//也可以将程序中的大量字符串输入到数据库的CLOB字段中
	//		ps.setClob(5, new BufferedReader(new InputStreamReader(new ByteArrayInputStream("aaaabbb111111bbb".getBytes()))));
			ps.executeUpdate();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	/**
	 * 查询数据信息 
	 * @param conn
	 * @param ps
	 * @param rs
	 * @param is
	 * @param os
	 * @param rd
	 */
	private static void selectInfo(Connection conn,PreparedStatement ps,ResultSet rs,InputStream is,OutputStream os,Reader rd) {
		
		try {
			ps = conn.prepareStatement("select * from user where id=?");
			ps.setObject(1, 1);
			rs = ps.executeQuery();
			while(rs.next()){
				Clob c = rs.getClob("textnode");
				rd  = c.getCharacterStream();
				Blob b = rs.getBlob("photo");
				is  = b.getBinaryStream();
				try {
					int temp1 = 0;
					while((temp1=rd.read())!=-1){
						System.out.print((char)temp1); //将数据库中的textnode数据读取出来
					}
					os = new FileOutputStream("f:/image/img/wyd.jpg"); //将数据库中的photo图片保存到一个路径
					int temp2 = 0;
					while((temp2=is.read())!=-1){
						os.write(temp2); 
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}

运行main方法后,在数据库中查看到添加的数据

控制台,打印出了textnode字段的信息:

在图片输出对应的文件夹下看到了数据库中的图片

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值