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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…