附属程序:
package com.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connec {
Connection con=null;
public Connec(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//加载数据库驱动(此驱动为jdbc:odbc驱动)
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Connection("jdbc:sqlserver://127.0.0.1:1433;databasename=star","sa","sa");//连接数据库,star数据库名,sa分别为账户名和密码
if(!con.isClosed()){
System.out.println("ok");
}else{
System.out.println("error");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getCon(){
return con;
}
}
主程序:
package com.db.main;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.db.Connec;
public class Win {
public static void main(String[] args) {
// TODO Auto-generated method stub
// @SuppressWarnings("unused")
// Add add=new Add();
@SuppressWarnings("unused")
Connection con=(new Connec()).getCon();
if(con!=null){
System.out.println("连接成功");
}
try {
FileOutputStream ous=new FileOutputStream(new File("C:\\zz.jpg"));
PreparedStatement ps=con.prepareStatement("select Image from Image where name=\'D:\\My Documents\\见证.jpg\'");//从数据库中读取的图片是事先已经存入的二进制数据
ResultSet rs=ps.executeQuery();
rs.next();
Blob ob=null;
Blob(1);
long size=ob.length();
byte bs[]=ob.getBytes(1, (int)size);
ous.write(bs);
ous.flush();
ous.close();
rs.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
java连接sqlserver数据库 } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论