参数是否可以一样?
package book.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.Statement;
import
java.sql.SQLException;
public class BinaryData {
/**
* 写进二进制数据到数据库
*/
public static void wirteBinary(Connection con){
String sql="insert dusha(name,address)values('john',?)";
PreparedStatement psm=null;
try{
//创建一个Statement,插入记录到数据库
psm=con.prepareStatement(sql);
//创建要写入的二进制数据
byte[] buffer="haidian district beijing china".getBytes();
psm.setBytes(1, buffer);
//插入
psm.executeUpdate();
}catch(
SQLException e){
e.printStackTrace();
}finally{
OperateDB.closeStatement(psm);
}
//从数据库中读出二进制数据
public static void readBinary(Connection con1){
String sql="select * from dusha where name='john'";
Statement sm=null;
try{
//查询数据库
Statement stmt=con.createStatement();
ResultSet resultSet=stmt.executeQuery(sql);
while(resultSet.next()){
//取值
byte[] bytes=resultSet.getBytes("address");
System.out.println(new String(bytes));
}
resultSet.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
OperateDB.closeStatement(sm);
}
}
}
public static void main(String[] args)throws
ClassNotFoundException,SQLException {
final String DBDRIVER="org.gjt.mm.mysql.Driver";
final String DBURL="jdbc:mysql://localhost/dusha";
String dbName="dusha";
String username="root";
String password="";
Connection con=null;
Connection con1=null;
try{
Class.forName(DBDRIVER);
con=DriverManager.getConnection(DBURL,username,password);
con1=DriverManager.getConnection(DBURL,username,password);
//写进二进制数据
BinaryData.wirteBinary(con);
BinaryData.readBinary(con);
}catch(Class
NotFoundException e1){
throw e1;
}catch(SQLException e2){
throw e2;
}finally{
OperateDB.closeConnection(con);
}
}
}
------解决方案--------------------
可以啊
------解决方案--------------------
不报错就可以呗