技术开发 频道

我与Db2 9新特性的零距离体验



    查询操作
    查询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; } }

0
相关文章