使用JDBC和ODBC连接集群 示例代码 此示例将演示如何基于DWS 提供的JDBC接口开发应用程序。 说明 在完成以下示例前,需要先创建存储过程。具体请参见《数据仓库服务用户开发指南》中的“教程:使用JDBC或ODBC开发”章节。 create or replace procedure testproc ( psvin1 in integer, psvin2 in integer, psvinout in out integer ) as begin psvinout : psvin1 + psvin2 + psvinout; end; / //DBtest.java //以下用例以gsjdbc4.jar为例。 //演示基于JDBC开发的主要步骤,会涉及创建数据库、创建表、插入数据等。 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; import java.sql.CallableStatement; import java.sql.Types; public class DBTest { //创建数据库连接,以下IP地址和database替换为对应的数据库连接地址和数据库名称。 public static Connection GetConnection(String username, String passwd) { String driver "org.postgresql.Driver"; String sourceURL "jdbc:postgresql://10.10.0.13:8000/database"; Connection conn null; try { //加载数据库驱动。 Class.forName(driver).newInstance(); } catch (Exception e) { e.printStackTrace(); return null; } try { //创建数据库连接。 conn DriverManager.getConnection(sourceURL, username, passwd); System.out.println("Connection succeed!"); } catch (Exception e) { e.printStackTrace(); return null; } return conn; }; //执行普通SQL语句,创建customert1表。 public static void CreateTable(Connection conn) { Statement stmt null; try { stmt conn.createStatement(); //执行普通SQL语句。 int rc stmt .executeUpdate("CREATE TABLE customert1(ccustomersk INTEGER, ccustomername VARCHAR(32));"); stmt.close(); } catch (SQLException e) { if (stmt ! null) { try { stmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //执行预处理语句,批量插入数据。 public static void BatchInsertData(Connection conn) { PreparedStatement pst null; try { //生成预处理语句。 pst conn.prepareStatement("INSERT INTO customert1 VALUES (?,?)"); for (int i 0; i < 3; i++) { //添加参数。 pst.setInt(1, i); pst.setString(2, "data " + i); pst.addBatch(); } //执行批处理。 pst.executeBatch(); pst.close(); } catch (SQLException e) { if (pst ! null) { try { pst.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //执行预编译语句,更新数据。 public static void ExecPreparedSQL(Connection conn) { PreparedStatement pstmt null; try { pstmt conn .prepareStatement("UPDATE customert1 SET ccustomername ? WHERE ccustomersk 1"); pstmt.setString(1, "new Data"); int rowcount pstmt.executeUpdate(); pstmt.close(); } catch (SQLException e) { if (pstmt ! null) { try { pstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } //执行存储过程。 public static void ExecCallableSQL(Connection conn) { CallableStatement cstmt null; try { cstmtconn.prepareCall("{? CALL TESTPROC(?,?,?)}"); cstmt.setInt(2, 50); cstmt.setInt(1, 20); cstmt.setInt(3, 90); cstmt.registerOutParameter(4, Types.INTEGER); //注册out类型的参数,类型为整型。 cstmt.execute(); int out cstmt.getInt(4); //获取out参数 System.out.println("The CallableStatment TESTPROC returns:"+out); cstmt.close(); } catch (SQLException e) { if (cstmt ! null) { try { cstmt.close(); } catch (SQLException e1) { e1.printStackTrace(); } } e.printStackTrace(); } } / 主程序,逐步调用各静态方法。 @param args / public static void main(String[] args) { //创建数据库连接,以下User、Password替换为实际连接的数据库用户和密码。 Connection conn GetConnection("User", "Password"); //创建表。 CreateTable(conn); //批插数据。 BatchInsertData(conn); //执行预编译语句,更新数据。 ExecPreparedSQL(conn); //执行存储过程。 ExecCallableSQL(conn); //关闭数据库连接。 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }