I execute this code and get an error:
package com.zetcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaPostgreSqlPrepared {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/testdb";
String user = "user12";
String password = "34klq*";
int id = 6;
String author = "Trygve Gulbranssen";
String query = "INSERT INTO authors(id, name) VALUES(?, ?)";
try (Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement pst = con.prepareStatement(query)) {
pst.setInt(1, id);
pst.setString(2, author);
pst.executeUpdate();
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(JavaPostgreSqlPrepared.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
I am Using PostgreSQL14, Java19, JDBC libraries included in NetBeans15.
Exception in thread "main" java.lang.RuntimeException: Uncompilable code - cannot find symbol
symbol: class JavaPostgreSqlPrepared
location: class insertalias.InsertAlias
at insertalias.InsertAlias.main(InsertAlias.java:1)
C:\Users\pruebas\AppData\Local\NetBeans\Cache\15\executor-snippets\debug.xml:150: The following error occurred while executing this line:
C:\Users\pruebas\AppData\Local\NetBeans\Cache\15\executor-snippets\debug.xml:129: The following error occurred while executing this line:
C:\Users\pruebas\AppData\Local\NetBeans\Cache\15\executor-snippets\debug.xml:77: Java returned: 1
BUILD FAILED (total time: 6 seconds)