#java #compiler-errors
#java #ошибки компилятора
Вопрос:
Я пытаюсь скомпилировать Java-программу, которая обращается к базе данных SQL, однако, когда я пытаюсь скомпилировать, я получаю это:
2410_bash_scripts % ./DemoDatabase.sh
src/main/java/demoDatabase/DemoDatabase.java:81: error: class, interface, or enum expected
try
^
src/main/java/demoDatabase/DemoDatabase.java:85: error: class, interface, or enum expected
int[] columnWidths = new int[columnCount 1];
^
src/main/java/demoDatabase/DemoDatabase.java:87: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:87: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:87: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:90: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:95: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:95: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:98: error: class, interface, or enum expected
System.out.printf(formatString, ResultSet.getObject(i));
^
src/main/java/demoDatabase/DemoDatabase.java:99: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:101: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:106: error: class, interface, or enum expected
e.printStackTrace();
^
src/main/java/demoDatabase/DemoDatabase.java:107: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:115: error: class, interface, or enum expected
System.out.print(" "); //to adjust for the 4 digit IDs
^
src/main/java/demoDatabase/DemoDatabase.java:117: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:117: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:117: error: class, interface, or enum expected
for(int i = 1; i <= columnCount; i )
^
src/main/java/demoDatabase/DemoDatabase.java:120: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:123: error: class, interface, or enum expected
} catch(SQLException e)
^
src/main/java/demoDatabase/DemoDatabase.java:126: error: class, interface, or enum expected
e.printStackTrace();
^
src/main/java/demoDatabase/DemoDatabase.java:127: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:130: error: class, interface, or enum expected
public static void main(String[] args)
^
src/main/java/demoDatabase/DemoDatabase.java:136: error: class, interface, or enum expected
System.out.println(studentList);
^
src/main/java/demoDatabase/DemoDatabase.java:138: error: class, interface, or enum expected
System.out.println("List of all students: ");
^
src/main/java/demoDatabase/DemoDatabase.java:139: error: class, interface, or enum expected
for(Student s : studentList)
^
src/main/java/demoDatabase/DemoDatabase.java:142: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:145: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:147: error: class, interface, or enum expected
public static List<Student> getAllStudents()
^
src/main/java/demoDatabase/DemoDatabase.java:151: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:151: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:151: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:157: error: class, interface, or enum expected
while(rs.next())
^
src/main/java/demoDatabase/DemoDatabase.java:160: error: class, interface, or enum expected
String firstName = rs.getString(2);
^
src/main/java/demoDatabase/DemoDatabase.java:161: error: class, interface, or enum expected
String lastName = rs.getString(3);
^
src/main/java/demoDatabase/DemoDatabase.java:162: error: class, interface, or enum expected
String major = rs.getString(4);
^
src/main/java/demoDatabase/DemoDatabase.java:163: error: class, interface, or enum expected
int gradYear = rs.getInt(5);
^
src/main/java/demoDatabase/DemoDatabase.java:165: error: class, interface, or enum expected
students.add(new Student(id, firstName, lastName, major, gradYear));
^
src/main/java/demoDatabase/DemoDatabase.java:166: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:172: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:174: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:176: error: class, interface, or enum expected
public static void printQueryResults(String... queries)
^
src/main/java/demoDatabase/DemoDatabase.java:178: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:178: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:183: error: class, interface, or enum expected
printResults(rs);
^
src/main/java/demoDatabase/DemoDatabase.java:184: error: class, interface, or enum expected
System.out.println();
^
src/main/java/demoDatabase/DemoDatabase.java:185: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:190: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:193: error: class, interface, or enum expected
public static void execute(String sqlStatement)
^
src/main/java/demoDatabase/DemoDatabase.java:195: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:195: error: class, interface, or enum expected
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
^
src/main/java/demoDatabase/DemoDatabase.java:200: error: class, interface, or enum expected
}
^
src/main/java/demoDatabase/DemoDatabase.java:204: error: class, interface, or enum expected
}
^
52 errors
Это мой код:
package demoDatabase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
//import java.util.*;
public class DemoDatabase
{
private static final String databaseUrl = "jdbc:derby:FirstDatabase;create = true";
/**
* Prints result set
*
* @param ResultSet
*/
private static void printResults(ResultSet ResultSet)
{
try
{
ResultSetMetaData metadata = ResultSet.getMetaData();
printHeader(metadata);
printDataSets(ResultSet, metadata);
}
catch (SQLException e)
{
System.out.println("A problem occurred accessing the metadata");
e.printStackTrace();
}
}
private static void printDataSets(ResultSet ResultSet, ResultSetMetaData metadata);
}
try
{
int columnCount = metadata.getColumnCount();
// create an array that stores the widths of the colums
int[] columnWidths = new int[columnCount 1];
for(int i = 1; i <= columnCount; i )
{
columnWidths[i] = metadata.getColumnLabel(i).length();
}
//print data records in straight columns
while(ResultSet.next())
{
for(int i = 1; i <= columnCount; i )
{
String formatString = "%-" columnWidths[i] "s ";
System.out.printf(formatString, ResultSet.getObject(i));
}
System.out.println();
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
System.out.println("A problem occured pritung the data records.");
e.printStackTrace();
}
}
private static void printHeader(ResultSetMetaData metadata)
{
try
{
int columnCount = metadata.getColumnCount();
System.out.print(" "); //to adjust for the 4 digit IDs
for(int i = 1; i <= columnCount; i )
{
System.out.print(metadata.getColumnLabel(i) " ");
}
System.out.println();
} catch(SQLException e)
{
System.out.println("A problem occurred accessing the metadata.");
e.printStackTrace();
}
}
public static void main(String[] args)
{
// executeQuery(SqlStudent.getAll(), SqlCollege.getAll(), SqlStudentCollege.getAll(),
// SqlStudentCollege.getStudentNamesAndColleges);
List<Student> studentList = getAllStudents();
System.out.println(studentList);
System.out.println("List of all students: ");
for(Student s : studentList)
{
System.out.println(s);
}
System.out.println("ndone.");
}
public static List<Student> getAllStudents()
{
List<Student> students = new ArrayList<>();
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
{
ResultSet rs = statement.executeQuery(SqlStudent.getAll());
// add students to list
//add data records to list
while(rs.next())
{
int id = (rs.getObject(1);
String firstName = rs.getString(2);
String lastName = rs.getString(3);
String major = rs.getString(4);
int gradYear = rs.getInt(5);
students.add(new Student(id, firstName, lastName, major, gradYear));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return students;
}
public static void printQueryResults(String... queries)
{
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
{
for(String query : queries)
{
ResultSet rs = statement.executeQuery(query);
printResults(rs);
System.out.println();
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public static void execute(String sqlStatement)
{
try (Connection connection = DriverManager.getConnection(databaseUrl); Statement statement = connection.createStatement();)
{
statement.execute(sqlStatement);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
Это файл bash, который я использую для его компиляции:
#! #!/usr/bin/env bash
cd /.../SqlDemo # excluding my full classpath here
javac -d bin -cp lib/derby.jar src/main/java/demoDatabase/DemoDatabase.java src/main/java/demoDatabase/SqlCollege.java src/main/java/demoDatabase/SqlStudent.java src/main/java/demoDatabase/SqlStudentCollege.java
java -cp bin:lib/derby.jar demoDatabase.DemoDatabase demoDatabase.SqlCollege demoDatabase.SqlStudent demoDatabase.SqlStudentCollege
У меня есть зависимость в моей папке lib, которая называется derby.jar это и есть база данных. Я просмотрел ошибку и я почти уверен, что это не фигурная скобка. Я даже не знаю, с чего начать с этой ошибкой, поскольку я никогда не получал ее раньше.
Комментарии:
1. Какая строка является строкой 81 в приведенном выше коде?
2. В вашем
printDataSets
методе после закрывающей круглой скобки указана ошибочная точка с запятой.3. И за ним следует a
}
вместо a{
.4. Я бы действительно посоветовал вам никогда не утверждать, что «Мой код выглядит нормально, поэтому он должен быть чем-то другим». Эта ошибка возникает только тогда, когда возникает какая-то проблема с кодом, хотя она может быть всего лишь одним символом, включая опечатку. Затем запускается огромная цепочка ошибок, которые устраняются при исправлении опечатки.
5. @NomadMaker достаточно справедливо.