-1

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)
user4157124
  • 2,809
  • 13
  • 27
  • 42
  • Post a log of the build, what command you used for it, and the run output as an edit to the question, please? – hd1 Oct 11 '22 at 20:24
  • 1
    The error you've added to the question tells us that the error is actually in the class `InsertAlias`. We can't tell you what's wrong with that class because you haven't included in your question. Please edit the question to include the source of this class as well. – Luke Woodward Oct 11 '22 at 20:50

1 Answers1

0

Name of the class was different from the name I was using in NetBeans. I copied the code directly from the page without trying to replicate the error.

user4157124
  • 2,809
  • 13
  • 27
  • 42