Java PostgreSQL:如何打印结果集?
你好,各位飞越者 如何打印结果集的列,例如“名称”(按编号顺序) 我被困在主方法@//的底部,无法从结果集中打印数据 谢谢你的帮助Java PostgreSQL:如何打印结果集?,java,sql,postgresql,resultset,Java,Sql,Postgresql,Resultset,你好,各位飞越者 如何打印结果集的列,例如“名称”(按编号顺序) 我被困在主方法@//的底部,无法从结果集中打印数据 谢谢你的帮助 import java.sql.*; import java.util.Scanner; import java.io.*; public class Database { public static Connection connectToDatabase(String user, String port, String dat
import java.sql.*;
import java.util.Scanner;
import java.io.*;
public class Database {
public static Connection connectToDatabase(String user, String port,
String database) {
System.out.println("-------- PostgreSQL JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? " + "Include in your library path!");
e.printStackTrace();
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:postgresql://localhost:" + port + "/" + database, user, "doesn't matter!");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
}
return connection;
}
public static ResultSet executeSelect(Connection connection, String query) {
Statement st = null;
try {
st = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
ResultSet rs = null;
try {
rs = st.executeQuery(query);
//st.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
return rs;
}
public static void dropTable(Connection connection, String table) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("DROP TABLE " + table);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void createTable(Connection connection,
String tableDescription) {
Statement st = null;
try {
st = connection.createStatement();
st.execute("CREATE TABLE " + tableDescription);
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static int insertIntoTableFromFile(Connection connection,
String table, String file) {
BufferedReader br = null;
int numRows = 0;
try {
Statement st = connection.createStatement();
String sCurrentLine, brokenLine[], composedLine = "";
br = new BufferedReader(new FileReader("src/TopURLs"));
while ((sCurrentLine = br.readLine()) != null) {
// Insert each line to the DB
brokenLine = sCurrentLine.split("\t");
composedLine = "INSERT INTO dotcom VALUES (";
int i;
for (i = 0; i < brokenLine.length - 1; i++) {
composedLine += "'" + brokenLine[i] + "',";
}
composedLine += "'" + brokenLine[i] + "')";
numRows += st.executeUpdate(composedLine);
//System.out.println(composedLine);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return numRows;
}
public static void print() {
System.out.println("########## 1st Query ##########");
}
public static void main(String[] argv) throws SQLException {
/*
Scanner input = new Scanner(System.in);
System.out.println("Please enter your Username:");
String user = input.next();
System.out.println("Please enter your Port ID:");
String port = input.next();
*/
String user = "zbva777";
String port = "28046";
String database = "test";
Connection connection = connectToDatabase(user, port, database);
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
return;
}
// Now we're ready to work on the DB
// connection is of type Connection (in JDBC)
DatabaseMetaData dbm = connection.getMetaData();
// check if table is there
ResultSet tables = dbm.getTables(null, null, "dotcom", null);
if (tables.next()) {
System.out.println("Table exists");
} else {
System.out.println("Table does not exist");
}
// check if view is there?
//"create view foo as select * from table;"
//"select * from foo;"
ResultSet views = dbm.getTables("catalog name", null, null, null);
if (views.next()) {
System.out.println("View exists");
} else {
System.out.println("View does not exist");
}
String query = "SELECT * FROM dotcom";
String view = "CREATE VIEW view as SELECT FROM dotcom";
ResultSet rs = executeSelect(connection, query);
try {
while (rs.next()) {
//System.out.print("Column 1 returned ");
//System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
dropTable(connection, "dotcom");
createTable(connection, "dotcom (rank int primary key, name varchar(128), type varchar(128), subtype varchar(128), subsubtype varchar(128));");
int rows = insertIntoTableFromFile(connection, "dotcom", "src/TopURLs");
System.out.println(rows + " rows inserted.");
//Print data from result set
print();
while(rs.next()) {
rs.getInt("rank");
String name = rs.getString("name");
System.out.println(name);
}
rs.close();
}
}
import java.sql.*;
导入java.util.Scanner;
导入java.io.*;
公共类数据库{
公共静态连接connectToDatabase(字符串用户、字符串端口、,
字符串(数据库){
System.out.println(“--PostgreSQL JDBC连接测试------------”;
试一试{
Class.forName(“org.postgresql.Driver”);
}catch(classnotfounde异常){
println(“您的PostgreSQL JDBC驱动程序在哪里?”+“包含在您的库路径中!”);
e、 printStackTrace();
}
System.out.println(“PostgreSQL JDBC驱动程序已注册!”);
连接=空;
试一试{
connection=DriverManager.getConnection(“jdbc:postgresql://localhost:“+端口+”/“+数据库,用户,“没关系!”;
}捕获(SQLE异常){
System.out.println(“连接失败!检查输出控制台”);
e、 printStackTrace();
}
回路连接;
}
公共静态结果集executeSelect(连接、字符串查询){
语句st=null;
试一试{
st=connection.createStatement();
}捕获(SQLE异常){
e、 printStackTrace();
返回null;
}
结果集rs=null;
试一试{
rs=st.executeQuery(查询);
//圣克洛斯();
}捕获(SQLE异常){
e、 printStackTrace();
返回null;
}
返回rs;
}
公共静态void dropTable(连接、字符串表){
语句st=null;
试一试{
st=connection.createStatement();
st.execute(“升降台”+升降台);
圣克洛斯();
}捕获(SQLE异常){
e、 printStackTrace();
}
}
公共静态void createTable(连接,
字符串表(描述){
语句st=null;
试一试{
st=connection.createStatement();
st.execute(“创建表”+表描述);
圣克洛斯();
}捕获(SQLE异常){
e、 printStackTrace();
}
}
公共静态int insertIntoTableFromFile(连接,
字符串表、字符串文件){
BufferedReader br=null;
int numRows=0;
试一试{
语句st=connection.createStatement();
字符串sCurrentLine,brokenLine[],composedLine=“”;
br=新的BufferedReader(新的文件读取器(“src/topURL”);
而((sCurrentLine=br.readLine())!=null){
//将每一行插入数据库
brokenLine=sCurrentLine.split(“\t”);
composedLine=“插入到互联网价值(”;
int i;
对于(i=0;i