在jsp页面中有个操作,实现的功能是:把数据库里的一个表的所有数据备份成一个文件,也就是打包;还要可以把数据再导入加载到数据库表里.
各位说说有什么好的方法.我的想法是把所有的记录查出来,然后一条条再写入一个文件.加载就再读取文件里的字段,插入数据库.但是我觉得这个方法效率不高,而且具体代码也不好写.
各位高手能不能给一些好的方法和代码.
各位说说有什么好的方法.我的想法是把所有的记录查出来,然后一条条再写入一个文件.加载就再读取文件里的字段,插入数据库.但是我觉得这个方法效率不高,而且具体代码也不好写.
各位高手能不能给一些好的方法和代码.
oracle数据库很容易 ,数据导入文件调用spool,文本文件导入用external table
别的数据库不熟悉,但我想本身也会有一些相应方便功能
String filepath,
String encrypt,
Connection conn) throws Exception
{
log.debug("creatFile(String, Connection) - start");
PreparedStatement pstamt = null;
ResultSet rs = null;
ResultSetMetaData rsmd = null;
StringBuffer databuffer = new StringBuffer();
PrintWriter out = null;
String sql = "select * form ";
int FILELINE = 1000 //如果记录大于1000就分文件存储;
File sysflie = new File(filepath + tablename+"_0" + ".txt"); try
{
out = new PrintWriter(new FileWriter(sysflie)); pstamt = conn.prepareStatement(sql + tablename);
log.debug("creatFile(String, Connection) - String sql=" + sql
+ parsername); rs = pstamt.executeQuery(); rsmd = rs.getMetaData(); int totalColumn = rsmd.getColumnCount(); log.debug("creatFile(String, Connection) - int totalColumn=" + totalColumn); // System.out.println("totalColumn=" + totalColumn);
int j = 0;
while (rs.next())
{
// System.out.println("rs.=\t" + rs.getString(1));
j = rs.getRow();
if(j%FILELINE==0)
{
int k = j/FILELINE;
sysflie = new File(filepath + tablename+"_"+k+ ".txt");
out = new PrintWriter(new FileWriter(sysflie));
}
databuffer.setLength(0);
for (int i = 1; i <= totalColumn - 1; i++)
{
if (rs.getString(i) != null)
{
databuffer.append(rs.getString(i).trim());
databuffer.append("\t");
}
else
{
databuffer.append("");
databuffer.append("\t");
} }
if (rs.getString(totalColumn) != null)
{
databuffer.append(rs.getString(totalColumn).trim());
}
else
{
databuffer.append("");
}
out.println(databuffer.toString());
}
out.close(); log.debug("creatFile(String, Connection) - int totalColumn=" + databuffer.toString());
}
catch (SQLException e)
{
log.error("creatFile(String, Connection)", e);
throw new SQLException("查询数据出错!");
}
catch (Exception e)
{
log.error("creatFile(String, Connection)", e);
throw new Exception ("生成系统文件出错!");
}
finally
{ }
}
要求是在通过jsp页面完成这样的操作.
out.close();
-----------------
这位兄弟再给导入的代码好吗.比如文件里存储的形式是这样的 :
2 发电 弗里德
23 分 扶绥大一行是数据库里的一条记录.那怎么读取'2'作为一个字段,然后再读取'发电'作为第二个字段,
还有怎么判断一行的结束.
谢谢了.
{
InputStreamReader read = null;
BufferedReader reader = null;
// PreparedStatement pstamt= null;
Statement stamt = null;
ResultSet rs = null;
// PreparedStatement pstamtfind = null;
// String sql = null;
TableTye tty = null;
String findsql = null;
String updatasql = null;
String insertsql = null;
int total = 0; try
{
/**
* 判断条件存在不;
*/
findsql = "select count(*) from "+tablename+" where ";
log.debug("readFile(File, String, Connection) - String findsql=" + findsql);
/**
* 更新存在的数据;
*/
updatasql ="update "+tablename+" set ";
insertsql = "insert into "+tablename+" values(";
//tty = new TableTye();
read = new InputStreamReader(new FileInputStream(datafile),
"GB2312");
reader = new BufferedReader(read); stamt = conn.createStatement(); String line; StringBuffer wheresql = new StringBuffer();
StringBuffer datasql = new StringBuffer();
StringBuffer insersql = new StringBuffer();
int count = 0; while ((line = reader.readLine()) != null)
{
String[] values = line.split("\t", 3);//有几个字段就用是几,这样在处理是不会出现null错误.
//这里就是你要构造的sql语句.
//tty.getFindSql(line,wheresql,datasql,insersql);
rs = stamt.executeQuery(findsql+wheresql.toString());
if(rs.next())
{
total = rs.getInt(1);
}
if(total==0)
{
// tty.getXmlFile(line,pstamt);
log.debug("insert sql:="+insertsql+insersql.toString()+")");
stamt.executeUpdate(insertsql+insersql.toString()+")");
// pstamt.executeUpdate();
}
else
{
// log.debug("aaaa:==="+updatasql+datasql.toString()+ "where "+wheresql.toString());
stamt.executeUpdate(updatasql+datasql.toString()+ "where "+wheresql.toString());
}
count++; }
message.append("文件名"+datafile.getName()+"文件数据导入成功!,成功导入:"+count+"条记录");
message.append("\n");
conn.commit();
} catch (Exception e)
{
try
{
conn.rollback();
}
catch (SQLException e1)
{
// TODO 自动生成 catch 块
e1.printStackTrace();
}
e.printStackTrace();
message.append("文件名"+datafile.getName()+"文件数据导入失败!");
message.append("\n");
throw new Exception ("导入文件失败");
}
finally
{
read = null;
reader = null;
// PreparedStatement pstamt= null;
stamt = null;
rs = null;
// PreparedStatement pstamtfind = null;
// sql = null;
tty = null;
findsql = null;
updatasql = null;
insertsql = null;
}
}
insersql = (value[0],value[1],value[2]);
datasql = name='value[1]';等这只是个例子.