Its giving me an error saying Column not found

2019-09-21 09:16发布

问题:

Its giving me an error saying Column not found

import java.sql.*;
class jdbc_demo
{
    public static void main(String args[])
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:testc");
            //Class.forName("com.mysql.jdbc.Driver");
            //Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db","root","abc");
            Statement st=con.createStatement();
            //st.execute("create table student(rollno int,name varchar(20),class varchar(10))");
            //st.execute("insert into student values(1,'Raj','Fy')");
            //st.execute("insert into student values(2,'Karan','Sy')");
            //st.execute("insert into student values(3,'sameer','Ty')");
            //st.execute("delete from student where name='xyza'");
            //st.executeUpdate("update student set name='xyza',class='Fy' where rollno=3");
            ResultSet rs=st.executeQuery("select*from student");
            ResultSetMetaData rsmd=rs.getMetaData();
            String c1=rsmd.getColumnName(1);
            String c2=rsmd.getColumnName(2);
            String c3=rsmd.getColumnName(3);
            System.out.println(c1+"\t"+c2+"\t"+c3);
            while(rs.next())
            {
                int r=rs.getInt("roll no");
                String n=rs.getString("name");
                String c=rs.getString(3);
                System.out.println(r+"\t"+n+"\t"+c);
            }
            con.close();
            System.out.println("Connection successfull");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}

回答1:

 int r=rs.getInt("roll no");  

Change this line of code to

 int r=rs.getInt("rollno");

Because your column is named rollno, not roll no.