Chinaunix首页 | 论坛 | 博客
  • 博客访问: 103650387
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类:

2008-04-24 09:52:07

作者: 宗一 出处:  

查询操作 

查询xml字段的类为readxml.java,该类通过提供cid来查询客户表中的info字段并产生xml文件。

package xml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class readxml ...{
private static Connection conn;

static Properties db2ConnProps = new Properties();
private static PreparedStatement poStmt;
private static ResultSet rs;
static Properties fileinputProps = new Properties();

public static void main(String[] args) throws FileNotFoundException,
IOException, SQLException ...{
String db, userName, passwd, host, port,description;

host = port = db = userName = passwd = null;
/**//* load the contents of properties file in case of missing arguments*/
db2ConnProps.load(new FileInputStream(
"F:\\eclipse\\WorkPlace\\DB2XML\\src\\db2Conn.properties"));
db = db2ConnProps.getProperty("databaseName");
userName = db2ConnProps.getProperty("userName");
passwd = db2ConnProps.getProperty("password");
host = db2ConnProps.getProperty("hostName");
port = db2ConnProps.getProperty("portNumber");

/**//** connect to the database **/
conn = db2Conn.get(db, userName, passwd, host, port);
int id = 99;
String fn = "F:\\eclipse\\WorkPlace\\DB2XML\\src\\Client8877.xml";
String sqls = "insert into customer ( cid, info) values (?, ?)";
File file = new File(fn);

try ...{
System.out.print(getDescription("88"));

} catch (RuntimeException e) ...{
// TODO Auto-generated catch block
e.printStackTrace();
} finally ...{
conn.close();
}

}

public static String getDescription(String cid)
...{
String description;
description=null;
try
...{
poStmt = conn.prepareStatement("Select INFO from CUSTOMER where cid=?");
poStmt.setString(1,cid);
rs= poStmt.executeQuery();
if(rs.next())
...{
description=rs.getString(1);
}
else
System.out.println(" Customer info not found.\n");
}
catch(SQLException sqle)
...{
System.out.println("Error Msg: "+ sqle.getMessage());
System.out.println("SQLState: "+sqle.getSQLState());
System.out.println("SQLError: "+sqle.getErrorCode());
System.out.println("Rollback the transaction and quit the program");
System.out.println();
try ...{conn.setAutoCommit(false);}
catch (java.sql.SQLException e)
...{
e.printStackTrace();
System.exit(-1);
}
try ...{ conn.rollback(); }
catch (Exception e)
...{
JdbcException jdbcExc = new JdbcException(e, conn);
jdbcExc.handle();
}
System.exit(1);
}
return description;
}
}


阅读(399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~