CLOB存取一个文本
import java. io. * ;
import java. sql. * ;
public class Main {
private static final String URL = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC" ;
private static final String USERNAME = "yucan" ;
private static final String PASSWORD = "1234" ;
public static void ClobDemo ( ) {
Connection connection= null ;
PreparedStatement pstatement= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
String sql= "insert into mynovel values(?,?)" ;
pstatement= connection. prepareStatement ( sql ) ;
pstatement. setInt ( 1 , 1 ) ;
File file= new File ( "C:\\Users\\22873\\Desktop\\软件测试\\all.txt" ) ;
InputStream inputStream= new FileInputStream ( file ) ;
Reader reader= new InputStreamReader ( inputStream, "UTF-8" ) ;
pstatement. setCharacterStream ( 2 , reader, ( int) file. length ( ) ) ;
int count= pstatement. executeUpdate ( ) ;
if ( count> 0 ) {
System. out. println ( "执行成功!" ) ;
}
reader. close ( ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( pstatement!= null )
pstatement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void ClobRead ( ) {
Connection connection= null ;
PreparedStatement pstatement= null ;
ResultSet resultSet= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
String sql= "select NOVEL from mynovel where id= ?" ;
pstatement= connection. prepareStatement ( sql ) ;
pstatement. setInt ( 1 , 1 ) ;
resultSet= pstatement. executeQuery ( ) ;
if ( resultSet. next ( ) ) {
Reader reader= resultSet. getCharacterStream ( "NOVEL" ) ;
Writer writer= new FileWriter ( "src\\小说.txt" ) ;
char[ ] chs= new char [ 100 ] ;
int len= - 1 ;
while ( ( len= reader. read ( chs) ) != - 1 ) {
writer. write ( chs, 0 , len ) ;
}
writer. close ( ) ;
reader. close ( ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( pstatement!= null )
pstatement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void main ( String[ ] args) {
ClobDemo ( ) ;
}
}
BLOB存取一个文本
import java. io. * ;
import java. sql. * ;
public class Main {
private static final String URL = "jdbc:mysql://localhost:3306/student?serverTimezone=UTC" ;
private static final String USERNAME = "yucan" ;
private static final String PASSWORD = "1234" ;
public static void BlobDemo ( ) {
Connection connection= null ;
PreparedStatement pstatement= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
String sql= "insert into mymusic values(?,?)" ;
pstatement= connection. prepareStatement ( sql ) ;
pstatement. setInt ( 1 , 1 ) ;
File file= new File ( "C:\\Users\\22873\\Desktop\\软件测试\\a.mp3" ) ;
InputStream inputStream= new FileInputStream ( file ) ;
pstatement. setBinaryStream ( 2 , inputStream, ( int) file. length ( ) ) ;
int count= pstatement. executeUpdate ( ) ;
if ( count> 0 ) {
System. out. println ( "执行成功!" ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( pstatement!= null )
pstatement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void BlobRead ( ) {
Connection connection= null ;
PreparedStatement pstatement= null ;
ResultSet resultSet= null ;
try {
Class. forName ( "com.mysql.cj.jdbc.Driver" ) ;
connection= DriverManager. getConnection ( URL , USERNAME , PASSWORD ) ;
String sql= "select music from mymusic where id= ?" ;
pstatement= connection. prepareStatement ( sql ) ;
pstatement. setInt ( 1 , 1 ) ;
resultSet= pstatement. executeQuery ( ) ;
if ( resultSet. next ( ) ) {
InputStream inputStream= resultSet. getBinaryStream ( "music" ) ;
OutputStream outputStream= new FileOutputStream ( "src\\music.mp3" ) ;
byte[ ] chs= new byte [ 100 ] ;
int len= - 1 ;
while ( ( len= inputStream. read ( chs) ) != - 1 ) {
outputStream. write ( chs, 0 , len ) ;
}
outputStream. close ( ) ;
inputStream. close ( ) ;
}
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
finally {
try {
if ( pstatement!= null )
pstatement. close ( ) ;
if ( connection!= null )
connection. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
}
public static void main ( String[ ] args) {
BlobDemo ( ) ;
}
}