Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect PostgreSQL to JAVA interface

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to connect PostgreSQL to the JAVA interface, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Installation

Before we start, we need to use PostgreSQL in the Java program to make sure that JDBC and Java for PostgreSQL are installed on the machine. You can check if Java is installed on the machine. Now, let's examine how to set up the JDBC driver.

Download the * * version of postgresql- (VERSION) .jdbc.jar from PostgreSQL's JDBC library.

Add the downloaded jar file postgresql- (VERSION) .jdbc.jar in the classpath, which you can use along the and-classpath option, as in the following example

If you don't know much about the concept of Java JDBC, it is recommended that you spend half an hour on the JDBC tutorial.

Connect to the database

The following Java code shows how to connect to an existing database. If the database does not exist, it will be created and a database object will finally be returned.

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");}}

After compiling and running the above program, find the pg_hba.conf file in the PostgreSQL installation directory and add the following line:

# IPv4 local connections: host all all 127.0.0.1/32 md5

Can start/restart Postgres the server in case it does not run using the following command:

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

Now let's compile and run the above program to get the testdb connection. Here, we use the postgresas user ID and password 123 to access the database. You can change this database configuration and settings. We also assume that the current version of the JDBC driver is postgresql- 9.2-1002.jdbc3.jar and is accessible in the current path.

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

Create a tabl

The following Java program will be used to create a table in the previously opened database. Make sure that this table has not been created in the target database.

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");}}

When the program is compiled and executed, it creates the database of the company table testdb and displays the following two lines:

Opened database successfully Table created successfully

INSERT operation

The following Java program shows how we created a record in the COMPANY table in the above example:

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");}}

When the program compiles and executes, it creates a record in the COMPANY table and displays the following two lines:

The above content of Opened database successfully Records created successfully is how to connect PostgreSQL to JAVA interface. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report