jdbc连接informix, SQL语句 绑定变量。

/* * cpp_wazi 20100919 */ import java.sql.*; import java.util.*; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Connection conn; String url = "jdbc:informix-sqli://host:9088/test:informixserver=dbserver;user=informix;password=1234"; System.out.println("Informix JDBC connect test."); try { // Load the Informix JDBC Driver Class.forName("com.informix.jdbc.IfxDriver"); // Create and open a server/database connection conn = DriverManager.getConnection(url); System.out.println("JDBC driver name: " + conn.getMetaData().getDriverName()); // Queries that return more than one row Statement query = null; ResultSet rs = null; String sSql_insert = "insert into test_cx select * from test_cx"; String sSql_update = "update test_cx set id = 9 where id = 44"; String sSql_delete = "delete from test_cx where id = 9"; //绑定变量执行SQL语句 String sPrepare = "select xh,xm,ke from xsb,cjb where xsb.xh=cjb.xh and xm=? Order by xh"; String sPrepare_insert = "insert into test_cx values(?,?)"; String sPrepare_delete = "delete from test_cx where id = ?)"; try { java.sql.PreparedStatement stmt = conn.prepareStatement(sPrepare_delete); stmt.setInt(1,1); int t = stmt.executeUpdate(); System.out.println(t); stmt.close(); conn.close(); } catch (SQLException exce) { System.out.println("EXE Caught: " + exce.getErrorCode()); } //正常执行SQL语句 try { query = conn.createStatement(); int t = query.executeUpdate(sSql_insert); System.out.println(t); } catch (SQLException exce) { System.out.println("EXE Caught: " + exce.getErrorCode()); } query.close(); conn.close(); } catch (ClassNotFoundException drvEx) { System.err.println("Could not load JDBC driver"); System.out.println("Exception: " + drvEx); drvEx.printStackTrace(); } catch (SQLException sqlEx) { while (sqlEx != null) { System.err.println("SQLException information"); System.err.println("Error msg: " + sqlEx.getMessage()); System.err.println("SQLSTATE: " + sqlEx.getSQLState()); System.err.println("Error code: " + sqlEx.getErrorCode()); sqlEx.printStackTrace(); sqlEx = sqlEx.getNextException(); } } } }

 

你可能感兴趣的:(sql,jdbc,String,delete,insert,Informix)