我在程序中找不到引用Course_number (CS1310, MATH1510)的正确函数。我试过使用getChar或getCharacter或GetCharAT,但仍然不工作,并得到了一个错误。拜托,有人能帮帮我。
错误:
线程"main"异常java.sql.SQLDataException: Cannot decide字符串"CS1310"的值类型
public class university123 {
public static void main(String args [])
throws SQLException, IOException, IOException{
try{
Class.forName("com.mysql.cj.jdbc.Driver");// maybe need to delete 'cj'
}catch (ClassNotFoundException x){
System.out.println ("Driver could not be loaded");
}
Scanner input = new Scanner(System.in);
String dbacct, passwrd, course_name,instructors;
Character course_number, grade, prerequisite_number; // *error
int credit_hours, Student_number;
System.out.println("Enter user name(root)");
String user = input.nextLine();
System.out.println("Enter password (of root)");
String pass = input.nextLine();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/university",user,pass);
String q = "select course_name, course_number, credit_hours from COURSE";
Statement s = conn.createStatement();
ResultSet r = s.executeQuery(q);
while (r.next()){
course_name = r.getString(1);
course_number = r.getCharacter(2); // *** error
credit_hours = r.getInt(3);
System.out.println(course_name + course_number + credit_hours);
}
}
}
你说:
course_number = r.getCharacter(2); // *** error
没有ResultSet#getCharacter
这样的方法。ResultSet
参见Javadoc
你说:
我已经尝试使用getChar或getCharacter或GetCharAT
这些方法对ResultSet
均无影响。与其胡乱猜测可能的方法名,为什么不阅读文档呢?
顺便说一下,你的代码还有其他问题。
char
类型及其包装器Character
类表示单个字符。我不明白你怎么打算用一个字符来表示课程编号,比如CS1310 &MATH1510 .
此外,您应该避免char
/Character
因为这种类型自Java 2以来就一直存在,基本上已经被破坏了。作为一个16位值,它在物理上无法表示大多数字符。对于处理单个字符,请使用码点整数。
最好关闭Connection
、Statement
和ResultSet
等资源。使用try-with-resources语法可以简化这个操作。
遵循Java命名约定,使您的代码更容易阅读。变量名以小写字母开头。我们通常会避免下划线。所以studentNumber
而不是Student_number
你说:
String q = "select course_name, course_number, credit_hours from COURSE";
我建议养成用分号正确结束SQL语句的习惯:String q = "select course_name, course_number, credit_hours from COURSE ;" ;
您从未在问题中提到该列的确切数据类型。在询问Java-SQL问题时,一定要注意确切的数据类型。
我只需要把信息放在"String": course_number = r.getString (2);
public class university123 {
public static void main(String args [])
throws SQLException, IOException, IOException{
try{
Class.forName("com.mysql.cj.jdbc.Driver");// maybe need to delete 'cj'
}catch (ClassNotFoundException x){
System.out.println ("Driver could not be loaded");
}
Scanner input = new Scanner(System.in);
String dbacct, passwrd, course_name,instructors, course_number, grade, prerequisite_number;
int credit_hours, Student_number;
System.out.println("Enter user name(root)");
String user = input.nextLine();
System.out.println("Enter password (of root)");
String pass = input.nextLine();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/university",user,pass);
String q = "select course_name, course_number, credit_hours from COURSE";
Statement s = conn.createStatement();
ResultSet r = s.executeQuery(q);
while (r.next()){
course_name = r.getString(1);
course_number = r.getString(2);
credit_hours = r.getInt(3);
System.out.println(course_name + course_number + credit_hours);
}
}
}