wood burning stoves*
The moose likes JSP and the fly likes about jsp and mysql database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

about jsp and mysql database

amanda wu
Greenhorn

Joined: Mar 16, 2003
Posts: 25
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

Joined: Mar 20, 2003
Posts: 13
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

Joined: Mar 16, 2003
Posts: 25
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?
 
Consider Paul's rocket mass heater.
 
subject: about jsp and mysql database