当前位置:网站首页>Opengauss database JDBC environment connection configuration (eclipse)

Opengauss database JDBC environment connection configuration (eclipse)

2022-06-11 15:50:00 Gauss squirrel Club

1. Test environment

Client system : Windows 10

Client software : eclipse 2020-09 

Server operating system :openEuler 20.03 64bit with ARM

database edition : openGauss 2.0.0 

2. Get ready

2.1 PC End installation configuration JDK11 

DOS Window type “java -version”, see JDK edition , Confirmed as JDK11 edition . If not installed JDK, please

Download the installation package from the official website and install .

Configure the system environment variables as follows :

a. Right click My computer , choice attribute .

b. stay System Click... On the left navigation bar of the page Advanced system setup .

c. stay System attribute page , senior Click... On the tab environment variable .

d. stay environment variable On the page , System variables Area Click newly build or edit “ Configure system variables .

2.2  download JDBC Drive and decompress

Download address :https://opengauss.obs.cn-south-1.myhuaweicloud.com/2.0.0/x86/openGauss-2.0.0-JDBC.tar.gz

3 Conduct eclipse To configure

start-up eclipse, Create a new project and add JDBC drive

 

Project name: openGauss-JDBC; JRE: JavaSE-11

  No need to create “Don’t Create”

 

Create a lib Directory in openGauss-JDBC Under the project

  hold jdbc Drive copy to lib Underside

 

  load jdbc drive

 “Add JARs”

 

  stay “Libraries” Next , Choose what you need postgresql.jar file , then “Apply and Close”

 Jdbc jar Has been loaded correctly , stay “Referenced Libraries” Next

  establish “Java Class”

 

  Copy the prepared code to java Class

package gaussjdbc;

//ogtest.java
// Presentation based on JDBC The main steps of development , It involves creating a database 、 Create table 、 Insert data, etc .

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.CallableStatement;

public class Gaussjdbc {

// Create database connection .
public static Connection GetConnection(String username, String passwd) {
String driver = "org.postgresql.Driver";
String sourceURL = "jdbc:postgresql://122.9.34.186:26000/db_tpcc";
Connection conn = null;
try {
  // Load database driver .
  Class.forName(driver).newInstance();
} catch (Exception e) {
  e.printStackTrace();
  return null;
}

try {
  // Create database connection .
  conn = DriverManager.getConnection(sourceURL, username, passwd);
  System.out.println("Connection succeed!");
} catch (Exception e) {
  e.printStackTrace();
  return null;
}

return conn;
};

// Carry out ordinary SQL sentence , establish customer_t1 surface .
public static void CreateTable(Connection conn) {
Statement stmt = null;
try {
  stmt = conn.createStatement();

  // Carry out ordinary SQL sentence .
  int rc = stmt
      .executeUpdate("CREATE TABLE customer_t1(c_customer_sk INTEGER, c_customer_name VARCHAR(32));");

  stmt.close();
} catch (SQLException e) {
  if (stmt != null) {
    try {
      stmt.close();
    } catch (SQLException e1) {
      e1.printStackTrace();
    }
  }
  e.printStackTrace();
}
}

// Execute preprocessing statements , Bulk insert data .
public static void BatchInsertData(Connection conn) {
PreparedStatement pst = null;

try {
  // Generate preprocessing statements .
  pst = conn.prepareStatement("INSERT INTO customer_t1 VALUES (?,?)");
  for (int i = 0; i < 3; i++) {
    // Add parameter .
    pst.setInt(1, i);
    pst.setString(2, "data " + i);
    pst.addBatch();
  }
  // Execute batch .
  pst.executeBatch();
  pst.close();
} catch (SQLException e) {
  if (pst != null) {
    try {
      pst.close();
    } catch (SQLException e1) {
    e1.printStackTrace();
    }
  }
  e.printStackTrace();
}
}

// Executing precompiled statements , Update data .
public static void ExecPreparedSQL(Connection conn) {
PreparedStatement pstmt = null;
try {
  pstmt = conn
      .prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");
  pstmt.setString(1, "new Data");
  int rowcount = pstmt.executeUpdate();
  pstmt.close();
} catch (SQLException e) {
  if (pstmt != null) {
    try {
      pstmt.close();
    } catch (SQLException e1) {
      e1.printStackTrace();
    }
  }
  e.printStackTrace();
 }
}



/**
*  The main program , Call each static method step by step .
* @param args
*/
public static void main(String[] args) {
// Create database connection .
Connection conn = GetConnection("joe", "[email protected]");

// Create table .
CreateTable(conn);

// Batch insert data .
BatchInsertData(conn);

// Executing precompiled statements , Update data .
ExecPreparedSQL(conn);

// Close database connection .
try {
  conn.close();
} catch (SQLException e) {
  e.printStackTrace();
}
}
}

  function java class “Run as -->java application”

Test sample code & Check the operation results

-- Check the client running results

-- Check for database data changes

The code ran successfully , And the database data changes normally , That is, the connection environment is configured .

 

 

原网站

版权声明
本文为[Gauss squirrel Club]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111535102392.html