为什么这段代码用字符流能把文本写入数据库,改用字节流不行?下面代码怎么修改用字节流把文本写入数据库
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;im...
import java.sql.Connection ;
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.PreparedStatement ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
public class ClobDemo01{
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "wen110" ;
public static void main(String args[]) throws Exception{
Connection conn = null ;
PreparedStatement pstmt = null ;
String name = "小白" ;
String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ;
File f = new File("e:" + File.separator + "javaText11.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
pstmt.setString(1,name) ;
pstmt.setBinaryStream(2,input) ;
pstmt.executeUpdate() ;
pstmt.close();
conn.close() ;
}
}; 展开
import java.sql.DriverManager ;
import java.sql.SQLException ;
import java.sql.PreparedStatement ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
public class ClobDemo01{
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "wen110" ;
public static void main(String args[]) throws Exception{
Connection conn = null ;
PreparedStatement pstmt = null ;
String name = "小白" ;
String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;
Class.forName(DBDRIVER) ;
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
pstmt = conn.prepareStatement(sql) ;
File f = new File("e:" + File.separator + "javaText11.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
pstmt.setString(1,name) ;
pstmt.setBinaryStream(2,input) ;
pstmt.executeUpdate() ;
pstmt.close();
conn.close() ;
}
}; 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询