Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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+"	"+c2+"	"+c3);
            while(rs.next())
            {
                int r=rs.getInt("roll no");
                String n=rs.getString("name");
                String c=rs.getString(3);
                System.out.println(r+"	"+n+"	"+c);
            }
            con.close();
            System.out.println("Connection successfull");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...