Java 向SQL Server插入文件数据
package sqlserver;
import java.util.Date;
import java.util.UUID; import java.text.SimpleDateFormat;
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.PrintStream;
import java.sql.*; public class sqlserver {
static String uuid=null;
//////////////////////////////////////////////////////
public static void main(String[] args) {
FileOutputStream bos = null;
try {
bos = new FileOutputStream("F:/output.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.setOut(new PrintStream(bos));
System.out.println(insertData());
// getData();
} public static Connection MSSQLConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=filesdata"; // 连接服务器和数据库test
String userName = "sa"; // 默认用户名
String userPwd = "sa"; // 密码
Connection dbConn = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println(df.format(new Date())+" "+"数据库连接成功。"); // 如果连接成功
return dbConn; // 控制台输出Connection
// Successful!
} catch (Exception e) {
e.printStackTrace();
}
return null;
} // read the file and insert into the video table;
static String insertData() {
int isFilesFound=0;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date2 = new Date();
Connection dbConn = null;
dbConn=MSSQLConnection();
//UUID uuid = UUID.randomUUID();
String f_id = UUID.randomUUID().toString();//用来生成数据库的主键id非常不错..
uuid=f_id;
File file = new File("F:/工作文件/PSD.rar");
System.out.println(df.format(new Date())+" "+"正在处理文件:"+file.getName()+" UUID:"+uuid);
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
isFilesFound=1;
} catch(FileNotFoundException e){
System.out.println(df.format(new Date())+" "+"未找到文件。"); }
if(isFilesFound==1){
try {
System.out.println(df.format(new Date())+" "+"开始向数据库写入文件。");
PreparedStatement ps = dbConn.prepareStatement("Insert into files (f_id,f_name,f_content,f_date) values (?,?,?,?)");
ps.setString(1,f_id);
ps.setString(2,file.getName());
ps.setBinaryStream(3,fis,(int)file.length());
ps.setDate(4,new java.sql.Date(date2.getTime())); System.out.println(df.format(new Date())+" "+"正在写入..。");
ps.executeUpdate();
ps.close();
try{
fis.close();
System.out.println(df.format(new Date())+" "+"文件已写入数据表且数据流已关闭。");
}catch(IOException e){
System.out.println(df.format(new Date())+" "+"数据流无法关闭。");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
dbConn.close();
System.out.println(df.format(new Date())+" "+"关闭数据库连接.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return df.format(new Date())+" 插入数据完成。";
} // get the data file from database;
static String getData(){
Connection dbcon = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
dbcon = MSSQLConnection();
//OutputStream out = new FileOutputStream("d:/1.exe");
Statement st = dbcon.createStatement();
ResultSet rs = st.executeQuery("select * from files where f_id = 'f7453b56-92f2-4aa9-8781-6ca85ab3f0ce'");
while(rs.next()){
java.io.InputStream fi=rs.getBinaryStream("f_content");
File file=new File("d:/1.zip");
FileOutputStream fo = new FileOutputStream(file);
BufferedOutputStream bo = new BufferedOutputStream(fo);
byte[] buff = new byte[1024];
System.out.println(df.format(new Date())+" "+"正在从数据库写入本地...");
while((fi.read(buff))>0)
{
bo.write(buff);
}
System.out.println(df.format(new Date())+" "+"写入完毕.");
bo.close();
fo.close();
fi.close();
System.out.println(df.format(new Date())+" "+"关闭读写流.");
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
if (dbcon!=null){
try{
dbcon.close();
System.out.println(df.format(new Date())+" "+"关闭数据库连接.");
}catch(Exception ex){
ex.printStackTrace();
}
}
dbcon = null;
}
return null; }
//////////////////////////////////////////////////////
}
Java 向SQL Server插入文件数据的更多相关文章
- SQL Server插入中文数据出现乱码问题
我在用sql server存储数据的时候发现中文全变成了问号,我知道中文是特殊的编码.所以在数据库设计的时候包含中文的字段就是nvarchar,但是还是成了问号 好了,不多说了,解决方案如下: 在存储 ...
- SQL Server插入中文数据后出现乱码
今天在做项目的过程中遇到如标题的问题,情况如下图: 数据库使用的是SQL Server2012版本,创建表的脚本如下: CREATE TABLE [dbo].[Type]( [TypeId] INT ...
- sql server 2005 大数据量插入性能对比
sql server 2005大数据量的插入操作 第一,写个存储过程,传入参数,存储过程里面是insert操作, 第二,用System.Data.SqlClient.SqlBulkCopy实例方法, ...
- sql server 与oracle数据互导的一种思路--sql server链接服务器
思路:通过在sql server数据库中添加链接服务器,可以远程查询oracle数据库的表环境准备,安装sql server数据库,并安装好oracle驱动,在配置好tnsname文件中配置好orac ...
- SQL Server :理解数据页结构
原文:SQL Server :理解数据页结构 我们都很清楚SQL Server用8KB 的页来存储数据,并且在SQL Server里磁盘 I/O 操作在页级执行.也就是说,SQL Server 读取或 ...
- 01. SQL Server 如何读写数据
原文:01. SQL Server 如何读写数据 一. 数据读写流程简要SQL Server作为一个关系型数据库,自然也维持了事务的ACID特性,数据库的读写冲突由事务隔离级别控制.无论有没有显示开启 ...
- SQL Server 如何读写数据
01. SQL Server 如何读写数据 一. 数据读写流程简要SQL Server作为一个关系型数据库,自然也维持了事务的ACID特性,数据库的读写冲突由事务隔离级别控制.无论有没有显示开启事 ...
- SQL Server数据库文件与文件组总结
文件和文件组概念 关于文件与文件组,简单概括如下,详情请参考官方文档"数据库文件和文件组Database Files and Filegroups"或更多相关资料: 数据文件概念: ...
- Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决
Java与SQL Server, MySql, Oracle, Access的连接方法以及一些异常解决 I. 概述 1.1 JDBC概念 JDBC(Java Database Connectivity ...
随机推荐
- 遇到delphi连接sql一个奇怪的问题:未指定的错误,加大了命令的等待时间为600即可了
遇到delphi连接sql一个奇怪的问题:未指定的错误,加大了命令的等待时间为600即可了 找了一下午没解决.
- 基于DDD的.NET项目搭建
第一次写博客有点小激动,废话不多说先上图: 01_Client:存放UI相关的项目,比如ASP.NET MVC或者相关的Web Model及View Model项目. 02_Hosting:存放与Se ...
- java基础<迷你DVD系统>
一.写在main方法中 import java.util.*; public class MyDVDMgr { public static void main(String[] args){ Scan ...
- C# Volatile 类
关于C#中Volatile类的具体信息,请访问MSDN: https://msdn.microsoft.com/en-us/library/system.threading.volatile(v=vs ...
- Xcode插件推荐
deriveddata-exterminator: A magic button in Xcode to exterminate the current project's DerivedData d ...
- 面向对象---final关键字
final 在java中表示的意思是最终,也称为完结器,可以用final关键字声明类.属性.方法,在声明时需要注意以下几点: 1.使用final声明的类不能有子类. 2.使用final声明的方法不能被 ...
- 计算机网络课程优秀备考PPT之第七章应用层(七)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...
- uoj 55 紫荆花之恋 动态点分治+替罪羊式重构+treap
每插入一个点,直接把它当做重心插入原树,当做是动态点分树一样维护 但这样深度会越来越大,所以我们用类似替罪羊的方法 当树失去平衡时,对子树进行一次点分,保证复杂度 #include <cstdi ...
- java实现线性表
/** * 线性表 * @author zyyt * */ public class LinkList {//框架级别的大师级 private int size;//链表的实际大小 private ...
- scrapy 抓取动态网页
-- coding: utf-8 -- ''' gouwu.sogou.com Spider, Created on Dec, 2014 version: 1.0 author: chenqx @ht ...