- package temp;
-
-
-
-
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- import org.postgresql.ds.PGSimpleDataSource;
-
- public class PostgreSQLDataSource {
-
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws SQLException {
- PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource();
- pgSimpleDataSource.setServerName("ip:11780");
-
-
-
- pgSimpleDataSource.setDatabaseName("foo");
-
-
- pgSimpleDataSource.setUser("sdbadmin");
- pgSimpleDataSource.setPassword("sa");
- Connection conn = pgSimpleDataSource.getConnection();
- Statement state = conn.createStatement();
-
- String sql = "insert into lfchat.auto_id_test (username,password,email) values (?,?,?);";
- PreparedStatement pstmt = (PreparedStatement) conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
- pstmt.setString(1, "username");
- pstmt.setString(2, "pwd");
- pstmt.setString(3, "email");
- pstmt.executeUpdate();
- ResultSet rs = pstmt.getGeneratedKeys();
- if (rs.next()) {
- int autoIncKey = rs.getInt(1);
- System.out.println("autoIncKey=>>>>>>>>>>" + autoIncKey);
- } else {
-
- }
-
- }
-
- static void showResultSet(ResultSet resultSet) throws SQLException {
- ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
- int num = resultSetMetaData.getColumnCount();
- while (resultSet.next()) {
- for (int i = 1; i <= num; i++) {
- System.out.print(resultSetMetaData.getCatalogName(i) + " "
- + resultSet.getString(i));
- }
- System.out.println();
- }
- }
- }