Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

java - Its giving me an error saying Column not found

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

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

1 Reply

0 votes
by (71.8m points)
 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...