62,614
社区成员
发帖
与我相关
我的任务
分享
public void saveArticles(){
checkConn();
String sql="insert into articles (article,time,zuoze) values(?,?,?)";
System.out.println(new Date().getTime());
File f=new File("src/net/BaseDao.java");
Reader rd=null;
try {
rd=new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int num=exeUpdt(sql, new Object[]{rd,new java.sql.Timestamp(new Date().getTime()),"bbs02"});
if(num==1){
System.out.println("成功传入了一条数据");
}else{
System.out.println("插入失败!");
}
closer();
}
public int exeUpdt(String sql,Object[] params){
int count=0;
try {
pstmt= conn.prepareStatement(sql);
if(params !=null){
for(int i=0;i<params.length;i++){
if(params[i] instanceof Reader){
pstmt.setCharacterStream(i+1, (Reader) params[i]);
continue;
}
pstmt.setObject(i+1, params[i]);
}
}
count= pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
net.sourceforge.jtds.jdbc.JtdsPreparedStatement.setCharacterStream(ILjava/io/Reader;)V
public class IniTest {
private Connection conn;
private Statement st;
private PreparedStatement pstatement;
private String driver;
private String url;
private String user;
private String password;
public void initParma(String paramFile) {
Properties pro = new Properties();
try {
pro.load(new FileInputStream(paramFile));
driver = pro.getProperty("driver");
url = pro.getProperty("url");
password = pro.getProperty("password");
user = pro.getProperty("user");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void createTable() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
File file = new File("d:/1.txt");
FileInputStream fis = new FileInputStream(file);
pstatement = conn.prepareStatement("insert into text_table values(null,?,?)");
pstatement.setString(1, "书名");
pstatement.setBinaryStream(2, fis, (int) file.length());
int count=pstatement.executeUpdate();
System.out.println("插入了"+count+"条数据");
}
catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (pstatement != null) {
try {
pstatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
IniTest iniTest = new IniTest();
iniTest.initParma("d:/mysql.ini");
iniTest.createTable();
}
}