2007年5月17日星期四

Java connect postgresql vic JDBC

import junit.framework.JUnit4TestAdapter;
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
 
/**
 * Created by IntelliJ IDEA.
 * User: ver
 * Date: 2007-5-17
 * Time: 16:04:27
 * To change this template use File | Settings | File Templates.
 */
public class PsqlConTest {
   
    /*public static junit.framework.Test suite() {
           return new JUnit4TestAdapter(MysqlConTest.class);
       }  */
       @Test
       public void linkt1() throws Exception
       {
       //  Class.forName("com.mysql.jdbc.Driver").newInstance();
      Class.forName("org.postgresql.Driver").newInstance();
       // Class.forName( "com.mysql.jdbc.Driver");
        Connection cn= DriverManager.getConnection("jdbc:postgresql://localhost:5432/school" ,"postgres","at46lantic");
        Statement sm=cn.createStatement();
        ResultSet rs=sm.executeQuery("SELECT * FROM TEACHER");
           while(rs.next())
           {
            System.out.println(rs.getString("name")  +"'s salaly is "+  rs.getString("salary"));
          //  System.out.println(rs.getString("salary"));
          //   System.out.println(rs.getString("age"));
           }
 
           rs.close();
           sm.close();
           cn.close();
       }
}
 
 
Your sincere,
------------
Henry Zhu
2007-05-18

没有评论: