package com.dnb.globalaccess;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcConnection {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
String url = "jdbc:db2://172.16.77.31:50007/ND01";
String user = "it07";
String pwd = "111111";
String sql = "SELECT * FROM CORP_MST FETCH FIRST 1 ROWS ONLY";
// 1.建立与数据库的连接:
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, pwd);
// 2.准备SQL
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = (ResultSet) ps.executeQuery();
// 3.输出
while (((ResultSet) rs).next()) {
System.out.println(rs.getString(1));
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcConnection {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
String url = "jdbc:db2://172.16.77.31:50007/ND01";
String user = "it07";
String pwd = "111111";
String sql = "SELECT * FROM CORP_MST FETCH FIRST 1 ROWS ONLY";
// 1.建立与数据库的连接:
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
Connection conn = DriverManager.getConnection(url, user, pwd);
// 2.准备SQL
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = (ResultSet) ps.executeQuery();
// 3.输出
while (((ResultSet) rs).next()) {
System.out.println(rs.getString(1));
}
}
}