loyalxuan 发表于 2016-11-19 07:44:54

postgreSQL jdbc连接

  import java.sql.*;
public class Dbtest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" this is a test " );
try 
  {
Class.forName( "org.postgresql.Driver" ).newInstance();
  String url = "jdbc:postgresql://localhost:5432/hktrade" ;
  System.out.println("aaa");
  Connection con = java.sql.DriverManager.getConnection(url, "postgres" , "Fourk8006" );
  if (con!=null) 
  {System.out.println("connect");}
  else
  {System.out.print("can't connect");};
   
  Statement st = con.createStatement();
  String sql = "select ucode ,password from poweruser " ;
  ResultSet rs = st.executeQuery(sql);
  System.out.print("maxrows:");
  while (rs.next())
  {
  System.out.print(rs.getString(1));
  System.out.println(rs.getString(2));
  } 
  rs.close();
  st.close();
  con.close();
   

  } 
  catch (Exception ee)
  {
  System.out.println("error"); 
  System.out.print(ee.getMessage());
  } 


}

}
页: [1]
查看完整版本: postgreSQL jdbc连接