📄 readme
字号:
This is a simple readme describing how to compile and use the jdbc driver.---------------------------------------------------------------------------This isn't a guide on how to use JDBC - for that refer to sun's web site: http://java.sun.com/For problems with this driver, then refer to the postgres-jdbc emaillist: http://www.postgresql.org/The Driver's home page is: http://jdbc.postgresql.org/---------------------------------------------------------------------------COMPILINGTo compile you will need to have ANT installed. To obtain ant go tohttp://jakarta.apache.org/ant/index.html and download the binary. Being purejava it will run on virtually all java platforms. If you have any problemsplease email the jdbc list.Once you have ANT, run the configure script in the top-level directory withthe --with-java option. Then proceed with 'make' and 'make install' asusual. This will compile the correct driver for your JVM, and build a .jarfile (Java ARchive) called postgresql.jar. The file will be installed inthe directory PREFIX/share/java.That jar file will contain the driver for _your_ version of the JDK.If you would like to use ANT directly, first invoke 'make build.properties'after running the configure script with the java option. This will create aneeded Java properties file from the configured information.REMEMBER: Once you have compiled the driver, it will work on ALL platformsthat support that version of the API. You don't need to build it for eachplatform.Possible problemsYou may see a message similar to:postgresql/Driver.java:87: interface java.sql.Connection is an interface. It can't be instantiated. return new Connection (host(), port(), props, database(), url, this);This is caused by not having the current directory in your CLASSPATH. UnderLinux/Solaris, unset the CLASSPATH environment variable, and rerun ant.If you are still having problems, prebuilt versions of the driver are available at http://jdbc.postgresql.org/---------------------------------------------------------------------------INSTALLING THE DRIVERTo install the driver, the .class files have to be in the classpath.ie: under LINUX/SOLARIS (the example here is my linux box): export CLASSPATH=.:/usr/local/pgsql/share/java/postgresql.jar---------------------------------------------------------------------------USING THE DRIVERTo use the driver, you must introduce it to JDBC. Again, there's two waysof doing this:1: Hardcoded. This method hardcodes your driver into your application/applet. You introduce the driver using the following snippet of code: try { Class.forName("org.postgresql.Driver"); } catch(Exception e) { // your error handling code goes here } Remember, this method restricts your code to just the postgresql database. However, this is how most people load the driver.2: Parameters This method specifies the driver from the command line. When running the application, you specify the driver using the option: -Djdbc.drivers=org.postgresql.Driver eg: This is an example of running one of my other projects with the driver: java -Djdbc.drivers=org.postgresql.Driver uk.org.retep.finder.Main note: This method only works with Applications (not for Applets). However, the application is not tied to one driver, so if you needed to switch databases (why I don't know ;-) ), you don't need to recompile the application (as long as you havent hardcoded the url's).---------------------------------------------------------------------------JDBC URL syntaxThe driver recognises JDBC URL's of the form: jdbc:postgresql:database jdbc:postgresql://host/database jdbc:postgresql://host:port/databaseAlso, you can supply both username and passwords as arguments, by appendingthem to the URL. eg: jdbc:postgresql:database?user=me jdbc:postgresql:database?user=me&password=mypassNotes:1) If you are connecting to localhost or 127.0.0.1 you can leave it out of the URL. ie: jdbc:postgresql://localhost/mydb can be replaced with jdbc:postgresql:mydb2) The port defaults to 5432 if it's left out.---------------------------------------------------------------------------That's the basics related to this driver. You'll need to read the JDBC Docson how to use it. However, there are some examples included in the exampledirectory. To build, type: make examplesTo run them, they follow the same syntax. For example, the basic example showshow to insert data, and perform queries: java example.basic jdbc:postgresql:test user password---------------------------------------------------------------------------POSTGRESQL SPECIFICS--------------------Large Objects:A "feature" of PostgreSQL is that access to LargeObjects is only permittedwithin a Transaction. Because of this, any use of LargeObjects (also knownas Blobs) requires that the Connection.setAutoCommit() method be calleddisabling the autocommit feature.For example: Connection db; // open the connection here db.setAutoCommit(false); // Turn off AutoCommit ------------------Large Object APIThe first thing you need to do is to open the LargeObjectManager. This classhandles the opening of existing objects, and creating new ones. To do this,you use the following line of code: LargeObjectManager lobj; lobj = ((org.postgresql.Connection)db).getLargeObjectAPI();where db is a reference to an open Connection object.Once that is done, you can use the API for the lifetime of that Connection.To create an object, you call the create() method. This takes an argumentwith the file modes you intend to use. The following line is normallysufficient: int oid = lobj.create(LargeObjectManager.READ|LargeObjectManager.WRITE);Here, lobj is the LargeObjectManager we have opened earlier, and oid is theLarge Object's oid in the database.To open an existing object, you use the open() method. This takes an oid, andthe file permissions. It then returns a LargeObject object. LargeObject obj = lobj.open(oid,LargeObjectManager.WRITE);Once the LargeObject is open, you can call methods to read, write, seek etc.Here's the supported methods: int oid = obj.getOID(); Return the objects oid obj.close(); Close the object byte data[] = obj.read(int len); Read len bytes onj.read(byte data[],int off,int len); Read into data[off] len bytes obj.write(byte data[]); Write the array data obj.write(byte data[],int off,int len); Write len bytes from data[off] obj.seek(int pos,int ref); As fseek in C. obj.seek(int pos); Move to pos (from the begining) int pos = obj.tell(); Returns the current position int size = obj.size(); Returns the objects sizeCaveat: If you commit(), rollback() a transaction, or turn on autocommit whilstan object is open PostgreSQL will close it. You will need to reopen the objectbefore using it again. Using the existing LargeObject will cause anSQLException to be thrown. ------------------JDBC supports database specific data types using the getObject() call. Thefollowing types have their own Java equivalents supplied by the driver: box, circle, line, lseg, path, point, polygonWhen using the getObject() method on a resultset, it returns a PG_Object,which holds the postgres type, and its value. This object also supportsmethods to retrive these types. Eg: column 3 contains a point, and rs is the ResultSet: PG_Object o = (PG_Object)rs.getObject(3); PGpoint p = o.getPoint(); System.out.println("point returned x="+p.x+", y="+p.y);Also, when using these classes, their toString() methods return the correctsyntax for writing these to the database.---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -