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 ...
随机推荐
- python自动化开发-2
1.python的数据类型之列表 列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作. 列表的定义:例子 names = ["Lucy",& ...
- sql Cursor的用法
table1结构如下 id int name ) declare @id int ) declare cursor1 cursor for --定义游标cursor1 select * from ta ...
- LR回放和录制设置
1.回放迭代设置:run-time settings 日志:扩展的-参数替换(常用).服务器返回数据.高级 2.回放时视图显示设置 设置 效果
- Linux 下安裝 MyEclipse
安裝 Java 運行環境:http://www.cnblogs.com/duanluan/p/5791726.html MyEclipse 下載地址:https://www.genuitec.com/ ...
- php 内置函数 ( 随手能写出100个才算高级工程师 )
string 类型 去掉左右空格, trim()字符串的长度 strlen() float类型数组类型 count() 数组个数 资源类型 fopen() 大写转小写 strtolower()小写转大 ...
- nyoj 79 拦截导弹 (动态规划)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...
- C#中的重写和覆盖的区别
#中重写(override)和覆盖(new)的区别 重写用关键字 virtual 修饰的方法,叫虚方法.可以在子类中用override 声明同名的方法,这叫“重写”.相应的没有用virtual修饰的方 ...
- 单身福利来了:VR恋人为你量身定制一个女朋友
相对于传统视频体验,VR视频给人带来了更加真实的体验.特别是对于单身狗来说,能在VR中拥有一个虚拟的恋人可谓是莫大的心灵安慰.近日,上海微雁文化传媒有限公司正式发布了首款养成类手机应用VR恋人. VR ...
- HOOK(钩子)函数
安装.卸载钩子的相关函数 钩子类型 按功能分: 1.WH_CALLWNDPROC和WH_CALLWNDPROCRET:使你可以监视发送到窗口过程的消息3.WH_DEBUG 调试钩子4.WH_FO ...
- hdu1722
链接 一份切成q份需要q刀,切成p份需要p刀:切的部分总会有重复,即gcd(p,q),减去重复部分就是要切的刀数 #include<stdio.h> int gcd(int n,int m ...