1.postgres数据库的select操作,返回select后返回的第一行数据,返回类型为string,若要返回int,只需将getString改为getInt
public static String PgDBSelect(String sql) throws ClassNotFoundException, SQLException{
String dbURL="jdbc:postgresql://172.16.4.25:5432/skyups";
String dbuser="***";
String dbpasswd="***";
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(dbURL,dbuser,dbpasswd);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
rs.next();
String value=rs.getString(1);
rs.close();
stmt.close();
conn.close();
return value;
}
2.postgres数据的执行操作,可执行instert、delete、update等操作,不返回数据
public static void PgDBUpdate(String sql) throws ClassNotFoundException, SQLException{
String dbURL="jdbc:postgresql://172.16.4.25:5432/skyups";
String dbuser="***";
String dbpasswd="***";
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(dbURL,dbuser,dbpasswd);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
3.oracle数据库的select操作,返回select后返回的第一行数据,若要返回int,只需将getString改为getInt
public static String OracleDBSelect(String sql) throws ClassNotFoundException, SQLException{
String dbURL="jdbc:oracle:thin:@172.16.4.21:1521:skytest";
String dbuser="***";
String dbpasswd="***";
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL,dbuser,dbpasswd);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
rs.next();
int expbalance=rs.getString(1);
rs.close();
stmt.close();
conn.close();
return expbalance;
}
4.oralce数据的执行操作,可执行instert、delete、update等操作,不返回数据
public static void OracleDBUpdate(String sql) throws ClassNotFoundException, SQLException{
String dbURL="jdbc:oracle:thin:@172.16.4.21:1521:skytest";
String dbuser="***";
String dbpasswd="***";
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL,dbuser,dbpasswd);
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
5.Cassandra非关系型数据库的操作,包括delete、insert、read等。
public static void CassandraRemove(String USkyid)throws IllegalStateException, PoolExhaustedException,Exception {
CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
CassandraClient client = pool.borrowClient("172.16.4.244", 9160);
try{
Keyspace keyspace = client.getKeyspace("skyups");
ColumnPath columnPath = new ColumnPath();
columnPath.setColumn_family("UserInfo");
//delete
keyspace.remove(USkyid, columnPath);
/*
// read
Column col = keyspace.getColumn(value, columnPath);
System.out.println("Read from cassandra: " + string(col.getValue()));
// insert
keyspace.insert("key", columnPath, bytes("value"));
*/
} finally{
pool.releaseClient(client);
}
}