没有错误,但记录未在postgres中创建

问题描述 投票:0回答:1
  package db;
  import java.sql.Connection;

  import java.sql.DriverManager;
import java.sql.PreparedStatement;

   import java.sql.SQLException;

 public class App

 { 

private  String database_connection_string = "jdbc:postgresql://localhost:2345/my_first_databse";

private  String database_user_name = "postgres";

private  String database_user_password = "postgres";


public Connection connect() {

    Connection conn = null;

    try {
         conn = DriverManager.getConnection(database_connection_string, 
         database_user_name,database_user_password );

        System.out.println("You are successfully connected to the PostgreSQL database server.");

  PreparedStatement stmt = conn.prepareStatement("insert into Table public.Employee(Eid , firstname 
    ,lastnamee, email)VALUES (3, raj, delhi,ha)");



            System.out.println("Recored Saved");


     //conn.close();

}//eof try//eof try

  catch (Exception e)

{

    System.err.println( e.getClass().getName()+": "+ e.getMessage() );

    System.exit(0);

    }//eof catch





 return conn;
  }//eof connect

  public static void main(String[] args) 

{

    App conn = new App();

   conn.connect();

  }//eof main

 }//eof class

此代码未显示错误,并插入了重新插入的消息,但是Employee表中没有保存数据* *pz给出建议,我错了,我要用jdbc连接数据.......................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ...................................................我希望您能帮助我,以便我能进阶到JdBC,看起来没有错误,但是为什么数据没有保存在数据库中image of pgadmin4

postgresql jdbc
1个回答
0
投票

您需要执行语句,而不仅仅是准备语句:

stmt.executeUpdate();

此外,作为一种实践,您不应假定连接自动提交,而应明确提交自己:

conn.commit();
© www.soinside.com 2019 - 2024. All rights reserved.