package testjdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.Timestamp;

import java.text.SimpleDateFormat;

import java.time.LocalDateTime;

public class testjdbc {

public static void main(String[] args) {

    Connection con;

    String driver = "org.postgresql.Driver";

    String url = "jdbc:postgresql://192.168.100.62:26000/testdb";

    String user = "test";

    String password = "test@ustb2020";

    try {

                        Class.forName(driver).newInstance();        

        con = DriverManager.getConnection(url,user,password);

                        System.out.println("数据库数据成功获取!!");        

                        if(!con.isClosed())

            System.out.println("Succeeded connecting to the openGauss:testdb");

        Statement statement = con.createStatement();

            java.util.Date date = new java.util.Date();    //创建时间对象

            Timestamp timeStamp = new Timestamp(date.getTime()); // 将日期时间转换为数据库中的timestamp类型

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            while(true){

                   Thread.sleep(1000);

                   timeStamp = Timestamp.valueOf(LocalDateTime.now());

                   String sql2 = "insert into test_tbl(InsertTime) values ('"+sdf.format(timeStamp)+"');";

                   statement.executeUpdate(sql2);

           }

//          rs.close();

         } catch(ClassNotFoundException e) {   

               System.out.println("Sorry,can`t find the Driver!");   

//              e.printStackTrace();   

              }

          catch(SQLException e) {

              e.printStackTrace();  

              }

         catch (Exception e) {

             // TODO: handle exception

              e.printStackTrace();

             }

        finally{

              System.out.println("异常退出!");

             }

      }

}

Logo

鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。

更多推荐