欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

use JDBC connact postgres on os of linux and windows,connactpostgres

来源: javaer 分享于  点击 47601 次 点评:40

use JDBC connact postgres on os of linux and windows,connactpostgres


LINUX 上操作

1. 版本信息

[xxx@localhost java]$ pg_ctl --version
pg_ctl (PostgreSQL) 9.3beta2

[xxx@localhost java]$ java -version
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) Client VM (build 23.21-b01, mixed mode)

2.JDBC安装见文章《linux 配置jdk 1.7》URL:http://blog.csdn.net/huguangshanse00/article/details/17322763

3. 示例 1

(1)Example1.java 内容

[xxx@localhost java]$ cat Example1.java

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


public class Example1 {
  public static void main(String[] argv) {
  System.out.println("Checking if Driver is registered with DriverManager.");


  try {
    Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException cnfe) {
    System.out.println("Couldn't find the driver!");
    System.out.println("Let's print a stack trace, and exit.");
    cnfe.printStackTrace();
    System.exit(1);
  }


  System.out.println("Registered the driver ok, so let's make a connection.");


  Connection c = null;
try {
    c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres","user", "passsword");
  }
  catch (SQLException se)
  {
    System.out.println("Couldn't connect: print out a stack trace and exit.");
    se.printStackTrace();
    System.exit(1);
  }
  if (c != null)
    System.out.println("Hooray! We connected to the database!");
  else
    System.out.println("We should never get here.");
  }
}


(2) 配置postgres JDBC驱动

从http://jdbc.postgresql.org/download.html 下载postgresql-9.3-1100.jdbc41.jar ,放在文件夹/home/wln/postgres9.3/install/share/java/下。

将内容export CLASSPATH=$CLASSPATH:/home/wln/postgres9.3/install/share/java/postgresql-9.3-1100.jdbc41.jar  添加到 ~/.bashrc,

并使其生效: source ~/.bashrc


(3) 执行命令

javac Example1.java

java -cp .:/home/wln/postgres9.3/install/share/java/postgresql-9.3-1100.jdbc41.jar  Example1

(1. 假如没执行(2))

[xxx@localhost java]$ java -cp .:/home/wln/postgres9.3/install/share/java/postgresql-9.3-1100.jdbc41.jar   Example1   

Checking if Driver is registered with DriverManager.
Registered the driver ok, so let's make a connection.
Hooray! We connected to the database!


(2. 若执行了(2))

[xxx@localhost java]$ java Example1
Checking if Driver is registered with DriverManager.
Registered the driver ok, so let's make a connection.
Hooray! We connected to the database!


4. 示例2

(1) code

[xxx@localhost ~]$ cat Example1.java
import  org.postgresql.* ;
import  java.sql. * ;
import  java.sql.* ;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


public class Example1 {
  public static void main(String[] argv) {
  System.out.println("Checking if Driver is registered with DriverManager.");


  try {
    Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException cnfe) {
    System.out.println("Couldn't find the driver!");
    System.out.println("Let's print a stack trace, and exit.");
    cnfe.printStackTrace();
    System.exit(1);
  }


  System.out.println("Registered the driver ok, so let's make a connection.");


  Connection c = null;
try {
    c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres","user", "password");
  }
  catch (SQLException se)
  {
    System.out.println("Couldn't connect: print out a stack trace and exit.");
    se.printStackTrace();
    System.exit(1);
  }
  if (c != null)
    System.out.println("Hooray! We connected to the database!");
  else
    System.out.println("We should never get here.");

  String sql="select * from t1";
  Statement st=null;
  ResultSet rs=null;

  try
  {
     st=c.createStatement();
     rs=st.executeQuery(sql);
     while(rs.next())
     {
        System.out.println(rs.getInt(1));
     }
    rs.close();
    st.close();
    c.close();


   }
   catch (SQLException e)
   {
      e.printStackTrace();
   }



  }
}



(2) 执行 javac Example1.java

java Example1


输出内容:

[xxx@localhost ~]$ java Example1
Checking if Driver is registered with DriverManager.
Registered the driver ok, so let's make a connection.
Hooray! We connected to the database!
1
2
3
4
5
6
7
8
9
10

 

5.示例3

import  org.postgresql.* ;
import  java.sql. * ;
import  java.sql.* ;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;


public class Example1 {
  public static void main(String[] argv) {
  System.out.println("Checking if Driver is registered with DriverManager.");


  try {
    Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException cnfe) {
    System.out.println("Couldn't find the driver!");
    System.out.println("Let's print a stack trace, and exit.");
    cnfe.printStackTrace();
    System.exit(1);
  }


  System.out.println("Registered the driver ok, so let's make a connection.");


  Connection c = null;
try {
    c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/postgres","postgres", "wl1111");
  }
  catch (SQLException se)
  {
    System.out.println("Couldn't connect: print out a stack trace and exit.");
    se.printStackTrace();
    System.exit(1);
  }
  if (c != null)
    System.out.println("Hooray! We connected to the database!");
  else
    System.out.println("We should never get here.");

  String sql="select * from t1";
  Statement st=null;
  ResultSet rs=null;

  try
  {
     st=c.createStatement();
     rs=st.executeQuery(sql);
     while(rs.next())
     {
        System.out.println(rs.getInt(1));
     }
    rs.close();
    st.close();
 


   }
   catch (SQLException e)
   {
      e.printStackTrace();
   }
   
   
   
  Statement s = null;
try {
  s = c.createStatement();
} catch (SQLException se) {
  System.out.println("We got an exception while creating a statement:" +
                     "that probably means we're no longer connected.");
  se.printStackTrace();
  System.exit(1);
}

int m = 0;

try {
  m = s.executeUpdate("INSERT INTO t1 VALUES " + "(11),(12),(13)");
   c.close();
} catch (SQLException se) {
  System.out.println("We got an exception while executing our query:" +
                     "that probably means our SQL is invalid");
  se.printStackTrace();
  System.exit(1);
}

System.out.println("Successfully modified " + m + " rows.\n");


  

  }
}



 

windows上操作


将postgres的jdbc驱动放到一个文件夹下,如D:\software7\postgresql-9.3-1100.jdbc3.jar; 添加环境变量classpath, 值为D:\software7\postgresql-9.3-1100.jdbc3.jar; 然后将上面的java程序同linux上操作运行即可。



参考文献:

1.http://www.commandprompt.com/ppbook/x20856.htm#CLASSNAMELOOKUP

2.http://blog.chinaunix.net/uid-20684384-id-1895193.html


相关文章

    暂无相关文章
相关栏目:

用户点评