•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 373,492 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,861 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 1058 | Replies: 7
![]() |
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
你好我这个程序是在学一本书时的一个例子,可是怎么也不能运行..总是那个代码说"类型不匹配"
大哥们帮一帮吧.我是小鸟...
opendata.jsp_____________
<%
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://localhost:3306/mydata";
Connection con=DriverManager.getConnection(url,"root","123");
Statement smt=con.createStatement();
ResultSet rs;
//int rs;
%>
counte.jsp_________________________________
<%@ page import ="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ include file="opendata.jsp"%>
<%@ page contentType="text/html;charset=GB2312"%>
<%
request.getSession(true);
int month,day,hour,today,total,select;
String time;
int sql;
String errmsg=request.getParameter("errmsg");
GregorianCalendar calendar;
calendar=new GregorianCalendar();
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
hour=calendar.get(Calendar.HOUR_OF_DAY);
time="m"+String.valueOf(month)+"d"+String.valueOf(day);
//sql="select * from counter where date='"+time+"'";
rs=smt.executeQuery("select * from counter where date='"+time+"'");
if(!rs.next());
{
sql="insert into counter(date,today,first,secon,third,forth,fifth,sixth) values('"+time+"',0,0,0,0,0,0,0)";
rs=smt.executeUpdate(sql);
}
//sql="select * from counter where date='"+time+"'";
rs=smt.executeQuery("select * from counter where date='"+time+"'");
today=rs.getInt(2);
out.print(today);
out.close();
select=(int)Math.floor(hour/4)+1;
//sql="select * from total";
rs=smt.executeQuery("select * from total");
total=rs.getInt(1);
if(session.isNew())
{
today++;
switch(select)
{
case 1: //sql="update counter set today="+today+",first=first+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",first=first+1 where date='"+time+"'");
break;
case 2: //sql="update counter set today="+today+",secon=secon+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",secon=secon+1 where date='"+time+"'");
break;
case 3: //sql="update counter set today="+today+",third=third+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",third=third+1 where date='"+time+"'");
break;
case 4: //sql="update counter set today="+today+",forth=forth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",forth=forth+1 where date='"+time+"'");
break;
case 5: //sql="update counter set today="+today+",fifth=fifth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",fifth=fifth+1 where date='"+time+"'");
break;
case 6: //sql="update counter set today="+today+",sixth=sixth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",sixth=sixth+1 where date='"+time+"'");
break;
}
total++;
//sql="update total set total=total+1";
smt.executeQuery("update total set total=total+1");
smt.close();
con.close();
}
%>
<html>
<title>人数浏览统计</title>
<p align-center><img src=zct.jpg><br>
<%
String showcount=String.valueOf(total);
out.print("您是本站的第");
for(int i=0;i<showcount.length();i++)
out.print("<img src="+showcount.charAt(i)+".jpg>");
out.print("位上网使用者<br>");
showcount=String.valueOf(today);
out.print("您是今天的第");
for(int i=0;i<showcount.length();i++)
out.print("<img src="+showcount.charAt(i)+".jpg>");
out.print("位上网使用者");
%>
<hr>
<center>
<form action-search.jsp method=post>
<table border=0>
<tr>
<td><font color=green>查询上网人数统计:<font></td>
<td><select size=1 name=month>
<option selected>1<option>2<option>3<option>4<option>5<option>
<option>6<option>7<option>8<option>9<option>10<option>11<option>12
</select>月
</td>
<td><select size=1 name=day>
<option selected>1<option>2<option>3<option>4<option>5<option>
<option>6<option>7<option>8<option>9<option>10<option>11<option>12
<option>13<option>14<option>15<option>16<option>17<option>
<option>18<option>19<option>20<option>21<option>22<option>23<option>24
<option>25<option>26<option>27<option>28<option>29<option>
<option>30<option>31
</select> 日
</td>
<td><input type=submit name=send value=查询></td>
</tr>
<tr>
<td colspan=4 align=center>
<a href=search.jsp?month=<%=month%>&day=<%=day%>>
<font size=3>查看今日稳中有降时段流量统计</font></a></td>
</tr>
</table>
</form>
<font color=red size=4>
<%
if(errmsg!=null)
out.print(errmsg);
%>
</font>
</html>
表结构
CREATE TABLE `counter` (
`date` char(6) DEFAULT NULL,
`today` int(6) DEFAULT NULL,
`first` int(6) DEFAULT NULL,
`secon` int(6) DEFAULT NULL,
`third` int(6) DEFAULT NULL,
`forth` int(6) DEFAULT NULL,
`fifth` int(6) DEFAULT NULL,
`sixth` int(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `total` (
`total` int(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
哪位大哥帮一帮..小弟谢谢了.
大哥们帮一帮吧.我是小鸟...
opendata.jsp_____________
<%
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://localhost:3306/mydata";
Connection con=DriverManager.getConnection(url,"root","123");
Statement smt=con.createStatement();
ResultSet rs;
//int rs;
%>
counte.jsp_________________________________
<%@ page import ="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ include file="opendata.jsp"%>
<%@ page contentType="text/html;charset=GB2312"%>
<%
request.getSession(true);
int month,day,hour,today,total,select;
String time;
int sql;
String errmsg=request.getParameter("errmsg");
GregorianCalendar calendar;
calendar=new GregorianCalendar();
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
hour=calendar.get(Calendar.HOUR_OF_DAY);
time="m"+String.valueOf(month)+"d"+String.valueOf(day);
//sql="select * from counter where date='"+time+"'";
rs=smt.executeQuery("select * from counter where date='"+time+"'");
if(!rs.next());
{
sql="insert into counter(date,today,first,secon,third,forth,fifth,sixth) values('"+time+"',0,0,0,0,0,0,0)";
rs=smt.executeUpdate(sql);
}
//sql="select * from counter where date='"+time+"'";
rs=smt.executeQuery("select * from counter where date='"+time+"'");
today=rs.getInt(2);
out.print(today);
out.close();
select=(int)Math.floor(hour/4)+1;
//sql="select * from total";
rs=smt.executeQuery("select * from total");
total=rs.getInt(1);
if(session.isNew())
{
today++;
switch(select)
{
case 1: //sql="update counter set today="+today+",first=first+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",first=first+1 where date='"+time+"'");
break;
case 2: //sql="update counter set today="+today+",secon=secon+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",secon=secon+1 where date='"+time+"'");
break;
case 3: //sql="update counter set today="+today+",third=third+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",third=third+1 where date='"+time+"'");
break;
case 4: //sql="update counter set today="+today+",forth=forth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",forth=forth+1 where date='"+time+"'");
break;
case 5: //sql="update counter set today="+today+",fifth=fifth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",fifth=fifth+1 where date='"+time+"'");
break;
case 6: //sql="update counter set today="+today+",sixth=sixth+1 where date='"+time+"'";
smt.executeQuery("update counter set today="+today+",sixth=sixth+1 where date='"+time+"'");
break;
}
total++;
//sql="update total set total=total+1";
smt.executeQuery("update total set total=total+1");
smt.close();
con.close();
}
%>
<html>
<title>人数浏览统计</title>
<p align-center><img src=zct.jpg><br>
<%
String showcount=String.valueOf(total);
out.print("您是本站的第");
for(int i=0;i<showcount.length();i++)
out.print("<img src="+showcount.charAt(i)+".jpg>");
out.print("位上网使用者<br>");
showcount=String.valueOf(today);
out.print("您是今天的第");
for(int i=0;i<showcount.length();i++)
out.print("<img src="+showcount.charAt(i)+".jpg>");
out.print("位上网使用者");
%>
<hr>
<center>
<form action-search.jsp method=post>
<table border=0>
<tr>
<td><font color=green>查询上网人数统计:<font></td>
<td><select size=1 name=month>
<option selected>1<option>2<option>3<option>4<option>5<option>
<option>6<option>7<option>8<option>9<option>10<option>11<option>12
</select>月
</td>
<td><select size=1 name=day>
<option selected>1<option>2<option>3<option>4<option>5<option>
<option>6<option>7<option>8<option>9<option>10<option>11<option>12
<option>13<option>14<option>15<option>16<option>17<option>
<option>18<option>19<option>20<option>21<option>22<option>23<option>24
<option>25<option>26<option>27<option>28<option>29<option>
<option>30<option>31
</select> 日
</td>
<td><input type=submit name=send value=查询></td>
</tr>
<tr>
<td colspan=4 align=center>
<a href=search.jsp?month=<%=month%>&day=<%=day%>>
<font size=3>查看今日稳中有降时段流量统计</font></a></td>
</tr>
</table>
</form>
<font color=red size=4>
<%
if(errmsg!=null)
out.print(errmsg);
%>
</font>
</html>
表结构
CREATE TABLE `counter` (
`date` char(6) DEFAULT NULL,
`today` int(6) DEFAULT NULL,
`first` int(6) DEFAULT NULL,
`secon` int(6) DEFAULT NULL,
`third` int(6) DEFAULT NULL,
`forth` int(6) DEFAULT NULL,
`fifth` int(6) DEFAULT NULL,
`sixth` int(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
CREATE TABLE `total` (
`total` int(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
哪位大哥帮一帮..小弟谢谢了.
I'm sorry, but I don't know how much help you're going to be able to find here in Chinese.
Edit: Or is that Japanese (the closer I look at it, the more I want to say Japanese).
Edit: Or is that Japanese (the closer I look at it, the more I want to say Japanese).
Last edited by masijade : Oct 30th, 2007 at 5:25 am.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
I'm sorry, but I don't know how much help you're going to be able to find here in Chinese.
Edit: Or is that Japanese (the closer I look at it, the more I want to say Japanese).
this is code ,I thank you can understand. my English is very bad.
you can tye run this code.I thank you can understand my mean.
thank you !!!
when you try code can run in your compter.please give me all you test code.
when I compter code .I can uderstand you mean.
make friend. that can study Engslish. AH!!!!
What I can understand about that code (without ever running it) is as follows:
1) You are using alot of scriptlets. This is bad. JSP is meant for the presentation layer (the view of an MVC), and nothing else. Move all of this scriptlet stuff to beans (or other context level items), and if any part of this JSP is to receive a post request (or file upload or any other request that requires "real" action), then that part should be a servlet that does the action, then forwards to a JSP for the presentation.
2) "org.gjt.mm.mysql.Driver" is the old (ancient really) mysql driver, use "com.mysql.jdbc.Driver"
3) don't do this
4) Use preparedStatements. Instead of
This is much more reliable than cobbling together your own statements, formats Dates, escapes Strings, and protects you from SQL Injection attacks.
5) Enclose all of the Database stuff in one or more try/catch blocks, closing the resultsets and statements (and connections if you get a new one each time) in a finally block, or you will leak DB resources. And when you do that, do not forget to actually do something with the Exception don't just do
6) Your table has absolutely no keys or indexes. Without keys, there is no way to prevent duplicate records (reliably and performantly), and without indexes (a key is also an index), if the table gets large, then all actions on the table will begin to take a long time.
7) Your second table, the one with a single field, I don't really see the purpose, but maybe that's only because I didn't really even attempt to see what your code is doing, but it seems a bit strange to me (but maybe it's only being used for persistence for a single value or something).
Well, have fun, and (even though I know you don't practice it) Happy Halloween!.
1) You are using alot of scriptlets. This is bad. JSP is meant for the presentation layer (the view of an MVC), and nothing else. Move all of this scriptlet stuff to beans (or other context level items), and if any part of this JSP is to receive a post request (or file upload or any other request that requires "real" action), then that part should be a servlet that does the action, then forwards to a JSP for the presentation.
2) "org.gjt.mm.mysql.Driver" is the old (ancient really) mysql driver, use "com.mysql.jdbc.Driver"
3) don't do this
time="m"+String.valueOf(month)+"d"+String.valueOf(day); In your statements (which we will get to in the next point) use a Date. If you absolutely must have the Date in a String format, then use SimpleDateFormat and a pattern to create it, don't cobble it together yourself.4) Use preparedStatements. Instead of
smt.executeQuery("update counter set today="+today+",first=first+1 where date='"+time+"'"); use stmt = conn.prepareStatement("update counter set today=? first=first+1 where date=?");
stmt.setInt(1, today);
stmt.setDate(2, new java.sql.Date(calendar.getTimeInMillis()));
stmt.execute();5) Enclose all of the Database stuff in one or more try/catch blocks, closing the resultsets and statements (and connections if you get a new one each time) in a finally block, or you will leak DB resources. And when you do that, do not forget to actually do something with the Exception don't just do
try { ... } catch (Exception e) {}6) Your table has absolutely no keys or indexes. Without keys, there is no way to prevent duplicate records (reliably and performantly), and without indexes (a key is also an index), if the table gets large, then all actions on the table will begin to take a long time.
7) Your second table, the one with a single field, I don't really see the purpose, but maybe that's only because I didn't really even attempt to see what your code is doing, but it seems a bit strange to me (but maybe it's only being used for persistence for a single value or something).
Well, have fun, and (even though I know you don't practice it) Happy Halloween!.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Find someone who can translate my advice for you, then start to implement it. I am not going to do it for you (if that is what "give me a complete reality then code" means, and I think it does). And as far as "Or helps me to fix thiscode" goes, I already did.
Last edited by masijade : Oct 31st, 2007 at 4:34 am.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Oct 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
Find someone who can translate my advice for you, then start to implement it. I am not going to do it for you (if that is what "give me a complete reality then code" means, and I think it does). And as far as "Or helps me to fix thiscode" goes, I already did.
stmt = conn.prepareStatement("update counter set today=? first=first+1 where date=?");
stmt.setInt(1, today);
stmt.setDate(2, new java.sql.Date(calendar.getTimeInMillis()));
stmt.execute();
how can find MYSQL filed data.
sample:rs="??????"
how can I get every note. in MYSQL.
if(!rs.next()); "rs" it need go find note.
or <%=rs("name")%> they need rs log data.
sql="update counter where date=?";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setDate(1, new java.sql.Date(calendar.getTimeInMillis()));
rs=stmt.execute();
up red "rs" can't use. it will display "An error occurred at line: 24 in the jsp file: /count.jsp
Type mismatch: cannot convert from boolean to ResultSet"
An "update" command does not produce a resultset. And, execute, as the error message clearly says, returns a boolean, not a resultset. And
<%=rs("name")%> doesn't mean anything. You shouldn't be using scriptlets, anyway, as I already said. Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JSP Marketplace
Similar Threads
- jsp and mysql (JSP)
- how to use jsp and tomcat (JSP)
- Can't run JSP in Browser (JSP)
- Passing arguments to jsp file (Java)
- Word.doc upload using JSP (Java)
- Can't run JSP in Browser (Web Browsers)
- JSP and Oracle (JSP)
Other Threads in the JSP Forum
- Previous Thread: problem in importing package
- Next Thread: submit button on JSP



Linear Mode