java:java中的数据库的练习,可以读…

package com.xudeyu.jdbc;

import java.awt.Image;
import java.awt.image.RenderedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.imageio.ImageIO;


public class TestDB
{

public static void main(String[] args) throws SQLException, IOException
{
DBClass db = new DBClass();
//db.JDBCTest();
db.lobInput("D:/eclipse/JDBCTest/src/22.jpg");
}
}

class DBClass
{
// TODO:全局的和构造函数的初始化级别

// 创建一个全局的conn对象,方便其他方法直接使用该对象
private static Connection conn;

public void DBClass() throws IOException, SQLException
{
conn = getConnection();
}

public void JDBCTest() throws SQLException, IOException
{
// 创建一个连接
Connection conn = getConnection();
try
{
// 创建一个报表
Statement stat = conn.createStatement();

// 新建,添加数据
stat
.executeUpdate("Create Table   TestTable(id char(20),image BLOB)");
stat
.executeUpdate("insert into TestTable values ('hello word!',null)");

// 查询结果集
ResultSet result = stat.executeQuery("select * from TestTable");

// 打印封装的Meta信息
ResultSetMetaData meteData = (ResultSetMetaData) result
.getMetaData();
int columnCount = meteData.getColumnCount();
for (int i = 1; i <= columnCount; i++)
{
if (i < 1)
System.out.print(",");
System.out.println("-- " + meteData.getColumnLabel(i));
}

// 循环调用Next
if (result.next())
{
// 打印第一个列
String data = result.getString(1);
System.out.println(data);

// 打印第二列
System.out.println(result.getString(2));
}
result.close();

// 删除创建的表
//stat.executeUpdate("drop table TestTable");
} catch (SQLException e)
{
e.printStackTrace();
} finally
{
conn.close();
}

}

public void lobOutput() throws IOException, SQLException
{
// 创建一个连接
Connection conn = getConnection();

// 预处理查询
PreparedStatement prestat = conn
.prepareStatement("SELECT ? FROM test where id=?");

// 对预处理查询赋值
prestat.setString(1, "image");
prestat.setInt(2, 1);

// 产生查询结果
ResultSet result = prestat.executeQuery();
if (result.next())
{
// 获取大对象流,将其转换为image;
Blob coverBlob = result.getBlob("image");
Image coverImage = ImageIO.read(coverBlob.getBinaryStream());
//FileOutputStream stream=new FileOutputStream(
// new BufferedOutputStream(coverBlob))
}
// 关闭流
result.close();
conn.close();

}

public void lobInput(String imageFile) throws IOException, SQLException
{
// 创建一个连接
Connection conn = getConnection();

// 创建一个大对象
Blob lobImage = conn.createBlob();

// 输出流
// TODO: Error
int offset = 1;
OutputStream out = lobImage.setBinaryStream(offset);
RenderedImage image = ImageIO.read(new File(imageFile));
ImageIO.write(image, "PNG", out);

// 预处理插入语句
PreparedStatement prestat = conn
.prepareStatement("insert into testTable(id,image) values(?,?)");

// 预处理插入赋值
prestat.setInt(1, 2);
prestat.setBlob(2, lobImage);

prestat.execute();
// 流关闭
prestat.close();
conn.close();
System.out.print("OK!");
}

public void moreResult() throws SQLException, IOException
{
// 创建一个连接
Connection conn = getConnection();
// TODO: stetment 最好为一个函数(给定查询语句)的返回值
// 创建一个报表
Statement stmt = conn.createStatement();

// 操作是否结束
boolean done = false;

// 是否有更多的结果
boolean isResult = stmt.execute("select * from qq");
while (!done)
{
if (isResult)
{
ResultSet result = stmt.getResultSet();
// do with result
} else
{
int updateCount = stmt.getUpdateCount();
if (updateCount >= 0)
{
// do with the update count
} else
done = true;
}
// 是否有更多的結果
isResult = stmt.getMoreResults();
}

}

private Connection getConnection() throws IOException, SQLException
{
// 获取databse.properties个性化配置
Properties props = new Properties();

// 获取个性化配置文件流
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close();

// 获取配置信息
String drivers = props.getProperty("jdbc.drivers");

if (drivers != null)
System.setProperty("jdbc.dreivers", drivers);

String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

// 返回数据库连接
return DriverManager.getConnection(url, username, password);
}

private void setProperties() throws IOException
{
// 获取个性化配置
Properties props = new Properties();

// 设置个性化配置的各个值
props.setProperty("jdbc.drivers", "com.mysql.jdbc.Drivers");
props.setProperty("jdbc.url", "jdbc:mysql://127.0.0.1:3306/test");
props.setProperty("jdbc.username", "root");
props.setProperty("jdbc.password", "");

// 固化个性化配置文件
FileOutputStream out = new FileOutputStream("database.properties");
props.save(out, "This is My data Base properties");
out.close();
}


}

<script type="text/javascript" id="wumiiRelatedItems"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值