Трудно увидеть, что пошло не так, не зная структуру вашей таблицы. Но вот еще один способ написания вашей программы. Возможно, это поможет вам приблизиться к цели:
public class Class1 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver"); // this line is optional since Java 1.6 Normally for MySQL you shouldn't have to do this
// This is called "try-with-resources". It means that inside your "try" declaration you
// tell Java what things you want to open (here you wnat to open a connection)
// and it will automatically close any connection (on failure or success) for you
try (
Connection connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/orders", // before the database name there is only one /
"root", // the database user
"password"); // the password
Statement statement = connection.createStatement() // the same try-with resources can open the statement for your
) {
ResultSet rs = statement.executeQuery("SELECT CUST_NAME FROM CUSTOMERS");
while (rs.next()) {
System.out.println(rs.getString("cust_name"));
}
} catch (SQLException ex) {
Logger.getLogger(Class1.class).log(Level.SEVERE, null, ex); // this will properly log your SQLException. Don't be afraid, SQL error messages are scary and Java Exceptions are impressive. But they will tell you where the problem is
}
} catch (ClassNotFoundException ex) { // if you register your driver you need to catch the exception as well
Logger.getLogger(Class1.class).log(Level.SEVERE, null, ex);
}
}
}
Подробнее об этом примере можно прочитать на MkYong .
Надеюсь, это поможет вам.
Еще одна вещь:
Имена классов начинаются с заглавной буквы: Class1
вместо class1
. Потому что имена переменных начинаются с маленькой буквы. Представьте, что вы хотите создать экземпляр класса с именем car
. Тогда вы скажете car car = new car();
, что невозможно прочитать. Car car = new Car()
однако ясно:)