婘潏傸嚔哋幋嬉潏傸驔襕婘Java䘋废婺嘪䫘PostgreSQL䇞媺PostgreSQL䔇JDBC启Java噾䂟婘橺単婪垬輙㔗埇傖演昖橺単婪滇劥垬輙庖Java㔗䯄婘螷潏傸演昖套嘘螆䘞JDBC鷌媘㔗 gitbook.net
傯PostgreSQL䔇JDBC康婋蘘橔桄䬽橸䔇 postgresql-(VERSION).jdbc.jar㔗
時媹婋蘘䔇jar桺傽postgresql-(VERSION).jdbc.jar婘䌂虇冇婺埇傖嘪䫘垄澪婯-classpath锬釹套婋麵冋床㔗 gitbook.net
套悩橬噿庯Java JDBC楗媕庖蓼婉滇冽崔闼幽傺螞誌剪償施庥幹JDBC嘺䇔昍䘋㔗
婋麵䔇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埇婘嘷嬉虇冇螪閞㔗
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 successfullygitbook.net
婋麵䔇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