i_am_birdman 发表于 2013-1-30 01:31:36

用JDBC连接Postgres(Postgres学习笔记1)

刚开始用Postgres。。
 
package Utils;import java.sql.*;importorg.postgresql. *;public class PostgrelDemo {public   static   voidmain(String args[])    {       System.out.print( " this is a test " );      try      {         Class.forName("org.postgresql.Driver").newInstance();         String url = " jdbc:postgresql://localhost:5432/mydb_1" ;         Connection con = DriverManager.getConnection(url, "postgres" , "1234" );         //String sql = " select * from students" ;         String sql = "insert into students (id,name) values(?,?)";          // ResultSet rs = st.executeQuery(sql);          //while (rs.next())            //{             //System.out.print(rs.getInt( 1 ));            // System.out.println(rs.getString( 2 ));         //}         //rs.close();         PreparedStatement pstmt = con.prepareStatement(sql);pstmt.setInt(1, 1225);pstmt.setString(2, "Nicole.rabbit");pstmt.executeUpdate();         pstmt.close();         con.close();       }      catch (Exception ee)      {         System.out.print("error at "+ee);       }   }}
页: [1]
查看完整版本: 用JDBC连接Postgres(Postgres学习笔记1)