日期:2014-05-17  浏览次数:20700 次

关于javaweb项目中连接数据库的问题!!
亲们!最近在学JavaWeb,感觉有点蛋疼。只能向大家求助了,改了一上午的代码,系统提示没有任何错误,但是在运行的时候总是达不到效果,下面我把代码罗列出来,大家帮我解决解决!我采用的MVC开发模式:
servlet代码:

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import bean.CtrlSql;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddVistList extends HttpServlet {
  private static final long serialVersionUID=1L; 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
CtrlSql con=new CtrlSql(); //实例化数据库操作类
String radio=request.getParameter("radio");
if (radio.equals("yes")) {
radio="是";
}
else {
radio="否";
}
String address=new String(request.getParameter("address").getBytes("iso8859-1"));
if (address==null) {
address="成都";
}
String name=new String(request.getParameter("name").getBytes("iso8859-1"));
String text=new String(request.getParameter("text").getBytes("iso8859-1"));  
DateFormat df=new SimpleDateFormat("yyyy-m-dd HH:mm:ss");
//定义格式化日期的格式
System.out.println("现在开始添加数据!!");
String date=df.format(new Date()); //格式化当前的时间,并赋给date
String sql="insert into 'laifang' values('" //(lf_name,lf_riqi,lf_liuyan,lf_gz,lf_address)  
+name 
+"','"
+date
+"','"
+text
+"','"
+radio
+"','"
+address+
"')";
//根据获得信息生成插入SQL语句
con.con(); //连接到数据库
con.update(sql); //执行SQl语句
response.sendRedirect("../iframe/message.jsp");//页面转向

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {  

}

下面这是jsp页面代码:
 <body style="margin-top: 0px;">
  <form action="../servlet/AddVistList" method="post">
  <table>
  <thead>
  <tr>
  <th colspan="2">
  <div style="text-align: right;"><a href=message.jsp>来访纪录</a></div>
  </th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <th>你是否是本校工作人员?</th>
  <td>
  是<input name="radio" type="radio" value="yes" checked="checked"/>
  否<input name="radio" type="radio" value="no"/>
  </td> 
  </tr>
  <tr>
  <th>你来自什么地方:</th>
  <td>
  <select