Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

about jsp and mysql database

 
amanda wu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Report post to moderator
the following code runs always give an exception
saying: first name
java.sql.SQLException: Column 'f' not found.
however, the mysql database content shown is:
mysql> select * from employees;
+---+
| f |
+---+
| 1 |
| 2 |
+---+
2 rows in set (0.02 sec)

any expert pls spend me a min for it,
since i have spend a whole day examing it.
thanks a lot!!
<html><head><title><Employee List</title></head>
<body>
<%@ page import ="java.sql.*" %>
<table border =1 width= "75%">
<tr><th>first name</th></tr>
<%
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=DriverManager.getConnection("jdbc:mysql://localhost/DB1");
st=conn.createStatement();
rs=st.executeQuery("Select * from employees");
while(rs.next()){
%>
<tr><td><%=rs.getString("f") %></td>
</tr>
<%
}
%>
</table>
<%
} catch (Exception ex){
ex.printStackTrace();
out.println(ex);
%>
</table>
wrong again:
<%
}finally{
if (rs!=null) rs.close();
if (st!=null) st.close();
if (conn!=null) conn.close();
}
%>
</body></html>
 
Rajat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Report post to moderator
firstly describe the column name in a table using
describe <tablename>
Mysql does not accept short name and spaces between a word like 'First name'. In fact every database should accept a word without any space.
after getting the column name, u can use that particular column name. or u can use integer value which specifies its position in a column.
e.g if first column is First_Name then u can retrieve value using rs.getString(1);
if it does not help u then post again.
 
amanda wu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Report post to moderator
many thanks to you,
it can print things out now,
but i input 1,2,3,4 in db,
the output is p,q,r,s,
do matter the it 1,2,3,4 is set as number or
as char..
can tell me why and how to solve it?
 
    Bookmark Topic Watch Topic
  • New Topic