Home > Software design >  NetBeans/JAVA/MySQL --- I do not know how to refer to a tuple with attribute "CS1310" or &
NetBeans/JAVA/MySQL --- I do not know how to refer to a tuple with attribute "CS1310" or &

Time:10-28

I cannot find the correct function to refer to Course_number (CS1310, MATH1510) in my program. I have tried to use getChar or getCharacter or GetCharAT but still not working and got a error all the time. Please, some one can help me. Thanks in advance.

Error:"Exception in thread "main" java.sql.SQLDataException: Cannot determine value type from string '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);    
        }
    }
            
}

CodePudding user response:

You said:

course_number = r.getCharacter(2); // *** error

No such method as ResultSet#getCharacter. See Javadoc for ResultSet.

You said:

I have tried to use getChar or getCharacter or GetCharAT

None of those are methods on ResultSet. Rather than guess wildly at possible method names, why not read the documentation?


By the way, your code has other problems.

The char type and its wrapper Character class represent a single character. I do not understand how you intend to use a single character to represent a course number such as CS1310 & MATH1510.

Also, you should avoid char/Character as that type has been legacy since Java 2, essentially broken. As a 16-bit value, it is physically incapable of representing most characters. For working with individual characters, use code point integer numbers.

Best to close resources such as Connection, Statement, and ResultSet. Using try-with-resources syntax makes that simple.

Follow Java naming conventions to make your code easier to read. Variables’ names begin with a lowercase letter. And we generally avoid underscores. So studentNumber rather than Student_number.

You said:

String q = "select course_name, course_number, credit_hours from COURSE";

I suggest making a habit of always terminating properly your SQL statements with a semi-colon: String q = "select course_name, course_number, credit_hours from COURSE ;" ;

I can see in lines such as that one above that you are mixing your case. I suggest your use all lowercase or all uppercase rather than using one on column names and another on table names. For maximum portability across databases, use all lowercase. Be aware that the SQL standard requires all databases to store names only in all-uppercase, but every database I know of disobeys that requirement. And, to avoid name collisions with any of the many reserved keywords, use a trailing underscore on all SQL identifiers. The SQL standard explicitly promises to never use a trailing underscore in any reserved word.

You never mention in your Question the exact data type of the column. Always note the exact data type when asking about Java-SQL questions.

CodePudding user response:

I just had to put the information as "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);    
    }
}
        

}

  • Related