Operate the PostgreSQL database in C, Java, PHP

zhaozj2021-02-11  237

Operate in C, Java, PHP PostgreSQL

Http://linuxaid.com.cn

Eight

Back to〗 Forward〗

PostgreSQL is easy to use non-ASQL, which describes several programming tools interact with PostgreSQL. 1.C Operation PostgreSQL program: / * Program in redhat6.0 debugging through * This program uses PGSQL internal functions to implement the general function of the database * Here Create, Insert, SELECT, UPDATE, DROP, several most common SQL statements * specific There are also more powerful features, such as blocking, etc., need further research * Details can view reference manuals, there are all function calls * / / * header file * / #include #include main () { Char * pghost, * pgport, * pgoptions, * pgtty; char * dbname; int nfields; int I, j; pgconn * conn; pgresult * res; / * * The program starts to set some parameters connected to the database server, if The set value is null, * uses the default value set in the environment variable.

* / Pghost = null; / * server host name * / pgport = null; / * server port * / pgOptions = null; / * Additional function parameters * / pgtty = null; / * server debug TTY * / DBNAME = "mytest"; / * To operate the database name * / / * connection database server * / conn = pqsetdb (pghost, pgport, pgoptions, pgtty, dbname); / * Check if the connection is successful * / if (PQSTATUS (CONN) = = Connection_bad) {fprintf (stderr, "connection to database database '% s' failed.", Dbname); fprintf (stderr, "% s", pqerrorMessage (conn)); EXIT_NICELY (CONN);} / * Start processing data block * / Res = pqexec (conn, "begin"); if (! Res || pqResultStatus (ras)! = Pgres_command_ok) {fprintf (stderr, "begin command failed); pqclear (res); exit_nicly (conn);} / * Call PQCLEAR in PQRESULT's cursor is no longer used to clear the cursor, prevent memory overflow * / pqclear (res); / * Create a table called TEST1 * / PQEXEC (CREATE TABLE TEST1 (Name Char (20), Age INT4) "); / * Insert data * / pqexec (CONN," INSERT INTO TEST1 VALUES ('CJM', 10) "); PQEXEC (Conn," INSERT INTO TEST1 VALUES ('EIGHT', 20) "); PQEXEC (CONN, "INSERT INTO TEST1 VALUES ('LinuxAid', 30)"); / * Start Query * / Printf ("All the date is:"); res = P QEXEC (CONN, "Select * from test1"); for (i = 0; i

) Printf ("% - 15s", pqgetvalue (res, "); printf (" ");} pqclear (res); / * Delete table * / pqexec (conn," drop table test1 "); / * Close and database connection * / pqfinish (conn); Let's discover after unpacking, it is actually a JAR file, add this JAR file to ClassPath. Note that I have been trapped here for a long time. I didn't need classpath when I installed JDK1.2, so I started I put the JAR file in the JDK directory, specify the path in the makefile file, and the dead attached CLASS NOT FOUND is always the ClassPath environment variable, it can be passed (don't ask me why I don't know, I don't know, :-(); Try {...} catch (..) {..} when using JDBC, otherwise I have an error, here to give a simple Sample, everyone HAPPY. Database used in the program Very simple, you can create it according to the following method (created in Postgres users): 1. Start the database: Postmaster -s -i 2. Create another user: CreateUser Eight 3. Create a database: createDb mytest 4. Create Table: First PSQL MyTest mytest => Create Table Test1 (Name Char (10), AGE INT (4)); note that the command is to be;

MYDB => Q Exit PSQL. Import java.lang. *; import java.util. *; import java.sql. *; public class db {public static void main (string argv []) {system.out.println (" Start ... "); // init Database Connection Connection PDB; Try {Class.Forname (" PostgreSql.driver ");} catch (java.lang.classnotfoundexception e) {system.out.println (" Err: Class. ");} try {pdb = drivermanager.getConnection (" JDBC: PostgreSQL: mytest "," EIGHT "," "); statement stmt = pdb.createstatement (); resultset = stmt.executeQuery (" SELECT * FROM Test1 "); if (rs! = null) {system.out.println (" GET DATA from Database: "); while (rs.next ()) {system.out.print (rs.getstring (1)); System.out.print (rs.getint (2)); system.out.print ("");}} r}; stmt.close (); pdb.close (); pdb.close ();} catch (Exception E) {E.PrintStackTrace ();}}} 3.php Operation PostgreSQL Use phplib to handle various databases, only need to expand phplib, add the PHPLIB file you want. PHPLIB operates a SQL database through a class named DB_SQL. In your code contains the version that is right for your database. In the example below, I use the PostgreSQL version. In order to get DB_SQL in the code, install the phplib file in the directory required by PHPLIB. Then, find the cgi-bin directory, create a phplib directory in the CGI-BIN directory. Then, copy all PHPLIB in the PHPLIB to the phplib directory. Finally, put the phplib directory on the line of include_path = in the php.ini file. Include_path is where PHP references are in include () or reguire (). At the top of each PHP page, it is <"PHP Require (Common.php3);" Common.php3 In the included directory, all the data and functions that are commonly used in each page.

转载请注明原文地址:https://www.9cbs.com/read-3615.html

New Post(0)