插入数据

最近更新时间: 2024-10-17 17:10:00

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class insert {
   public static void main(String args[]) {
      Connection c = null;
      Statement stmt = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:15432/postgres?binaryTransfer=false","tbase", "tbase");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");
         stmt = c.createStatement();
         String sql = "INSERT INTO public.tbase (id,nickname) "
               + "VALUES (1,'tbase');";
         stmt.executeUpdate(sql);

         sql = "INSERT INTO tbase (id,nickname) "
               + "VALUES (2, 'pgxz' ),(3,'pgxc');";
         stmt.executeUpdate(sql);
         stmt.close();
         c.commit();
         c.close();
      } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
      }
      System.out.println("Records created successfully");
   }
}