我在eclipse中有一个警告,以及如何在最后导入时删除它:import java.sql.SQLException; //从未使用import java.sql.SQLException site of this JDBC example
//JdbcTestpostgreSQL.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
class JdbcTestPostgreSQL {
public static void main (String args[]) {
try {
Class.forName("org.postgresql.Driver");
}
catch (ClassNotFoundException e) {
System.err.println (e);
System.exit (-1);
}
try {
// open connection to database
Connection connection = DriverManager.getConnection(
//"jdbc:postgresql://dbhost:port/dbname", "user", "dbpass");
"jdbc:postgresql://192.168.0.40:5432/postgres", "postgres", "postgres");
// build query, here we get info about all databases"
String query = "SELECT datname FROM pg_database WHERE datistemplate = false";
// execute query
Statement statement = connection.createStatement ();
ResultSet rs = statement.executeQuery (query);
// return query result
while ( rs.next () )
// display table name
System.out.println ("PostgreSQL Query result: " + rs.getString ("datname"));
connection.close ();
}
catch (java.sql.SQLException e) {
System.err.println (e);
System.exit (-1);
}
}
}
答案 0 :(得分:1)
这是因为你的catch子句中有catch(java.sql.SQLException e)
。由于您使用的是完全限定名称,因此不需要导入。
如果您将其更改为catch(SQLException e)
,则警告将消失。