java XML成功存入DB2 代码的实际操作步骤,假如你在实际操作中java XML成功存入DB2 代码的实际操作,但是你却不知道对其如何正确的对其进行操作的话,那么以下的文章对你而言一定是良师益友。
1、首先现在DB2上建立表:
Create table xmltable(id int , content xml);
- 1.
2、执行下面代码把c盘下的XML文件存入数据库
package X2R2D;
import java.io.*;
import java.sql.*;
public class xml2db2
{
private Connection con = null;
private PreparedStatement pstat=null;
public boolean openConn() throws Exception
{
try{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
try{Class.forName("com.ibm.db2.jcc.DB2Driver");}catch(Exception e){System.out.println("驱动失败");}
String url="jdbc:db2://××××××";
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
数据库名
String user="******"; //用户名
String password="******"; //密码
con=DriverManager.getConnection(url, user, password);
return true;
}
catch(SQLException e)
{
e.printStackTrace();
return false;
}
}
public void getInsert() throws Exception
{
pstat= con.prepareStatement("INSERT INTO xmltable VALUES (?,?)");
String xmlfile="c:/1.xml";
String xmlString = "";
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
从文件中读取XML数据,构造成String类型的数据try{
InputStreamReader isr = new InputStreamReader(new FileInputStream(xmlfile), "UTF-8");
- 1.
注意编码
BufferedReader in = new BufferedReader(isr);
String line =null;
while((line = in.readLine()) !=null)
{
xmlString += line;
}
in.close();
pstat.setInt(1, 2);
pstat.setString(2, xmlString);
pstat.executeUpdate();
}
public boolean closeConn() throws SQLException
{
con.close();
return true;
}
public static void main(String[] args)throws Exception
{
xml2db2 db=new xml2db2();
db.openConn();
db.getInsert();
db.closeConn();
System.out.println("XML成功写入DB2数据库");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
以上的相关内容就是对java XML成功存入DB2 代码的介绍,望你能有所收获。
【编辑推荐】