接着上一篇文章,成功安装完PostgreSQL数据库服务器后,就是开始用myeclipse6.5连接PostgreSQL数据库的java程序了。


package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.postgresql.*;
public class testdb {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println( " this is a test " );
try
{
System.out.println( " try link db " );
Class.forName( "org.postgresql.Driver" ).newInstance();
//Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://localhost:5432/postgres" ;
Connection con = DriverManager.getConnection(url, "postgres" , "step" );
Statement st = con.createStatement();
String sql = " select * from testtable " ;
ResultSet rs = st.executeQuery(sql);
System.out.println( " try link db " );
while (rs.next())
{
System.out.println( " try show db " );
System.out.print(rs.getInt( 1 ));
System.out.println(rs.getString( 2 ));
}
rs.close();
st.close();
con.close();
}
catch (Exception ee)
{
System.out.print(ee.getMessage());
}
}
}

本文介绍如何使用MyEclipse 6.5通过Java程序连接PostgreSQL数据库。首先需要导入必要的jar包,并通过org.postgresql.Driver加载PostgreSQL驱动。然后设置数据库URL、用户名和密码,创建Connection对象进行连接。
3879

被折叠的 条评论
为什么被折叠?



