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