欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-05 08:14:45
...

刚开始用Postgres。。

 

package Utils;
import java.sql.*;

import  org.postgresql. *;
public class PostgrelDemo {
	
	public   static   void  main(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);
       }
   }
}