JDBC 直连TPYE 4 +绑定变量范例

package study;

import java.sql.*;

public class getOracleConnection {

	public static void main(String[] args) throws SQLException {
		int i;
		i = 0;
		String v_sql;
		String Table_name;
		String Staus;
		Table_name = "SHEBEI_HOST2";
		Staus = "VALID";
		DB2 db = new DB2();
		Connection conn = db.dbConnect(
				"jdbc:oracle:thin:@192.168.2.56:1521/orcl", "dao", "dao");
		v_sql = "select * from user_tables where table_name= ? and status = ?";
		PreparedStatement stmt = (PreparedStatement) conn
				.prepareStatement(v_sql);
		stmt.setString(1, Table_name);
		stmt.setString(2, Staus);
		ResultSet rs = stmt.executeQuery();
		while (rs.next()) {
			i = i + 1;

			System.out.println("表数量: " + rs.getString(1));

		}
	}
}

class DB2 {
	public DB2() {
	}

	public Connection dbConnect(String db_connect_string, String db_userid,
			String db_password) {
		try {
			DriverManager.registerDriver(new oracle.jdbc.OracleDriver());

			Connection conn = DriverManager.getConnection(db_connect_string,
					db_userid, db_password);

			System.out.println("connected");
			return conn;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
};

你可能感兴趣的:(JDBC 直连TPYE 4 +绑定变量范例)