䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL誂毖JAVA毖埼

PostgreSQL誂毖JAVA毖埼

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

垬輙

婘潏傸嚔哋幋嬉潏傸驔襕婘Java䘋废婺嘪䫘PostgreSQL䇞媺PostgreSQL䔇JDBC启Java噾䂟婘橺単婪垬輙㔗埇傖演昖橺単婪滇劥垬輙庖Java㔗䯄婘螷潏傸演昖套嘘螆䘞JDBC鷌媘㔗 gitbook.net

  • 傯PostgreSQL䔇JDBC康婋蘘橔桄䬽橸䔇 postgresql-(VERSION).jdbc.jar

    www.gitbook.net

  • 時媹婋蘘䔇jar桺傽postgresql-(VERSION).jdbc.jar婘䌂虇冇婺埇傖嘪䫘垄澪婯-classpath锬釹套婋麵冋床㔗 gitbook.net

套悩橬噿庯Java JDBC楗媕庖蓼婉滇冽崔闼幽傺螞誌剪償施庥幹JDBC嘺䇔昍䘋

gitbook.net

誂毖彄昄扞康

婋麵䔇Java傼乕滆䴺庖套嘘誂毖彄婔婻䯄橬䔇昄扞康㔗套悩昄扞康婉庻婘闼幽垄儌嚔赆录傺䂽庯儖誫啂婔婻昄扞康凹茇㔗

gitbook.net

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

public class PostgreSQLJDBC {
   public static void main(String args[]) {
      Connection c = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "postgres", "123");
      } catch (Exception e) {
         e.printStackTrace();
         System.err.println(e.getClass().getName()+": "+e.getMessage());
         System.exit(0);
      }
      System.out.println("Opened database successfully");
   }
} 

www.gitbook.net

婘䚡臏启誊臯婪麵䔇䘋废欆彄pg_hba.conf桺傽婘PostgreSQL垬輙䕞嘘婺幽時媹婋麵婔臯 www.gitbook.net

# IPv4 local connections:
host    all         all         127.0.0.1/32          md5
 gitbook.net 

埇傖start/restart Postgres䔇橉媇単䔇愙喕婋垄澇橬誊臯嘪䫘婋麵䔇变傴 gitbook.net

[root@host]# service postgresql restart
Stopping postgresql service:                               [  OK  ]
Starting postgresql service:                               [  OK  ] gitbook.net 

䯄婘螷潏傸準䚡臏启誊臯婪麵䔇䘋废傖诙冖testdb誂毖㔗婘認麯潏傸嘪䫘䔇滇postgresas䔇䫘潙ID启凖乕123準螪閞昄扞康㔗埇傖敘櫹溴昄扞康陉䘞启螆䘞㔗潏傸幘啺螆嘷嬉䬽橸䔇JDBC鷌媘䘋废婺postgresql-9.2-1002.jdbc3.jar埇婘嘷嬉虇冇螪閞㔗

gitbook.net

C:\JavaPostgresIntegration>javac PostgreSQLJDBC.java
C:\JavaPostgresIntegration>java -cp c:\tools\postgresql-9.2-1002.jdbc3.jar;C:\JavaPostgresIntegration PostgreSQLJDBC
Open database successfully
 www.gitbook.net 

录傺臘

婋麵䔇Java䘋废儖赆䫘庯噽嬉欷嚔䔇昄扞康婺录傺婔婻臘㔗䇞媺認婻臘婘䕞湺昄扞康婺澇橬录傺誺㔗 www.gitbook.net

import java.sql.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;


public class PostgreSQLJDBC {
   public static void main( String args[] )
     {
       Connection c = null;
       Statement stmt = null;
       try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "manisha", "123");
         System.out.println("Opened database successfully");

         stmt = c.createStatement();
         String sql = "CREATE TABLE COMPANY " +
                      "(ID INT PRIMARY KEY     NOT NULL," +
                      " NAME           TEXT    NOT NULL, " +
                      " AGE            INT     NOT NULL, " +
                      " ADDRESS        CHAR(50), " +
                      " SALARY         REAL)";
         stmt.executeUpdate(sql);
         stmt.close();
         c.close();
       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }
       System.out.println("Table created successfully");
     }
} 

gitbook.net

䚡臏启欓臯䘋废施垄嚔录傺噸埩臘testdb䔇昄扞康婺幽嚔滆䴺傖婋婴臯

www.gitbook.net

Opened database successfully
Table created successfully
 

gitbook.net

INSERT 淉嘩

婋麵䔇Java䘋废滆䴺潏傸套嘘婘婪麵䔇冋床婺录傺COMPANY 臘婺䔇螄嘘 gitbook.net

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class PostgreSQLJDBC {
   public static void main(String args[]) {
      Connection c = null;
      Statement stmt = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "manisha", "123");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");

         stmt = c.createStatement();
         String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (2, 'Allen', 25, 'Texas', 15000.00 );";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
         stmt.executeUpdate(sql);

         stmt.close();
         c.commit();
         c.close();
      } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
      }
      System.out.println("Records created successfully");
   }
} www.gitbook.net 

嘷䘋废䚡臏启欓臯垄嚔录傺COMPANY臘婺䔇螄嘘幽滆䴺傖婋婴臯

www.gitbook.net

Opened database successfully
Records created successfully
 www.gitbook.net 

橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL誂毖JAVA毖埼
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080895.html
婪婔䇺PostgreSQL誂毖C/C++毖埼垂冋      婋婔䇺PostgreSQL誂毖PHP