六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 150|回复: 0

postgreSQL jdbc连接

[复制链接]

升级  74%

41

主题

41

主题

41

主题

秀才

Rank: 2

积分
161
 楼主| 发表于 2013-1-30 01:30:16 | 显示全部楼层 |阅读模式
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());
  } 


}

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表