import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLWarning;
import java.sql.Statement;
public class GetValueResultSet {
public static void main(String[] args) throws Exception{
Connection con = getConnection();
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//st.executeUpdate("create table survey(id char(5)not null,name char(30)not null,primarykey(id));");
//st.executeUpdate("insert into orders (id,custid,total) values ('13','Names',3000)");
//st.executeUpdate("insert into orders (id,custid,total) values ('21','kang',5000)");
st = con.createStatement();
ResultSet rs = st.executeQuery("select * from orders");
outputResultSet(rs);
checkForWarning(rs.getWarnings());
rs.close();
st.close();
con.close();
}
private static void outputResultSet(ResultSet rs) throws Exception{
ResultSetMetaData rsmd = (ResultSetMetaData) rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
for(int i = 1; i < numberOfColumns; i ++){
String columnName = rsmd.getColumnName(i);
System.out.print(columnName + " ");
}
System.out.println();
System.out.println("----------------------");
while(rs.next()){
for(int i =1; i < numberOfColumns; i++){
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
static boolean checkForWarning(SQLWarning w){
if(w == null){
return false;
}
do{
System.err.println("Warning:\nMessage: " + w.getMessage());
System.err.println("SQL state: " + w.getSQLState());
System.err.println("Vendor code: " + w.getErrorCode() + "\n----------------");
}while((w = w.getNextWarning()) != null);
return true;
}
public static Connection getConnection() throws Exception{
Connection con = null;
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby:C:/Program Files/Sun/JavaDB/bin/vsjdb;create=true");
return con;
}
}
使用SQLWarning20.27.10.Using SQLWarning
最新推荐文章于 2023-06-16 15:56:53 发布