Jdbc 既然没有任何错误,我能知道为什么它没有给出下面代码的输出吗?
既然没有任何错误,我能知道为什么它没有给出下面代码的输出吗Jdbc 既然没有任何错误,我能知道为什么它没有给出下面代码的输出吗?,jdbc,Jdbc,既然没有任何错误,我能知道为什么它没有给出下面代码的输出吗 任何帮助都将不胜感激我认为您在DriverManager.getConnection参数中遗漏了端口号,应该如下所示 package labexercise2; import java.sql.*; public class LabExercise2 { public static void main (String [] args) throws SQLException, ClassNotFoundException {
任何帮助都将不胜感激我认为您在DriverManager.getConnection参数中遗漏了端口号,应该如下所示
package labexercise2;
import java.sql.*;
public class LabExercise2
{
public static void main (String [] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println ("Driver Loaded");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/restaurant?"+ "user=root&password=");
System.out.println ("Database connected");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT MenuID, MenuName, Type, Cuisine FROM menu WHERE Price BETWEEN 7 AND 13");
while (rs.next()){
System.out.println(rs.getString("MenuID") + " : " +
rs.getString("MenuName") + " : " +
rs.getString("Type") + " : " +
rs.getString("Cuisine") + " : " +
rs.getString("Price")); }
connection.close();
}
}
我认为您在DriverManager.getConnection参数中遗漏了端口号,它应该如下所示
package labexercise2;
import java.sql.*;
public class LabExercise2
{
public static void main (String [] args) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println ("Driver Loaded");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/restaurant?"+ "user=root&password=");
System.out.println ("Database connected");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT MenuID, MenuName, Type, Cuisine FROM menu WHERE Price BETWEEN 7 AND 13");
while (rs.next()){
System.out.println(rs.getString("MenuID") + " : " +
rs.getString("MenuName") + " : " +
rs.getString("Type") + " : " +
rs.getString("Cuisine") + " : " +
rs.getString("Price")); }
connection.close();
}
}
包装实验2;
导入java.sql.*
公开课练习2
{
公共静态void主字符串[]args抛出SQLException、ClassNotFoundException{
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant?"+ "user=root&password=");
}
}包装标签练习2;
导入java.sql.*
公开课练习2
{
公共静态void主字符串[]args抛出SQLException、ClassNotFoundException{
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant?"+ "user=root&password=");
}
}您可以尝试以下代码来代替连接。关闭
您可以尝试以下代码来代替连接。关闭
我已经模拟并测试了你的代码,它对我有效,如下所述
if (statement != null) {
statement.close();
}
if (connection!= null) {
connection.close();
}
}我已经模拟并测试了您的代码,它对我有效,如下所述
if (statement != null) {
statement.close();
}
if (connection!= null) {
connection.close();
}
}您的意思是说,您无法看到表中的字段,或者println没有工作???它只在输出面板中显示运行:驱动程序加载的数据库连接生成成功总时间:0秒。我使用的是netbeans 8.1,我无法看到应该像在executeQuery中一样执行的表字段。前两个println成功显示放置brekpoint并调试代码。查看您的查询是否返回任何值。首先,您没有在查询中选择价格,而是尝试打印价格。您的意思是,您无法看到表的字段,或者println都不工作???它只在输出面板中显示运行:驱动程序加载的数据库连接生成成功总时间:0秒。我使用的是netbeans 8.1,我无法看到应该像在executeQuery中一样执行的表字段。前两个println成功显示放置brekpoint并调试代码。查看您的查询是否返回任何值。首先,您没有在查询中选择价格,而是尝试打印价格。是否可以在while循环之前打印rs对象?线程主java.sql.SQLException中的异常:对空结果集的非法操作。空结果集。??听起来你的程序代码运行得很好。请参阅我先前对您原始问题的评论。我认为您的select查询值得怀疑。请先在Mysql工作台中尝试查询。特别是,我怀疑你在哪里的条件,以找到价值的范围。请检查DB price字段的类型是否为single或double。是否可以在while循环之前打印rs对象?线程main java.sql.SQLException中的异常:对空结果集的非法操作。空结果集。??听起来你的程序代码运行得很好。请参阅我先前对您原始问题的评论。我认为您的select查询值得怀疑。请先在Mysql工作台中尝试查询。特别是,我怀疑你在哪里的条件,以找到价值的范围。检查DB价格字段是否为单或双类型。是否认真?你创建了数据库吗?怎么可能?是的,我确实创建了小DB样本来调试这个问题。真的吗?你创建了数据库吗?怎么可能?是的,我确实创建了small DB sample来调试该问题。我尝试后仍无法获得输出:我尝试后仍无法获得输出: