代码测试bjx

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("异常退出!");

             }

      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值