首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

关于javaweb项目中连接数据库的有关问题!

2012-03-24 
关于javaweb项目中连接数据库的问题!!亲们!最近在学JavaWeb,感觉有点蛋疼。只能向大家求助了,改了一上午的

关于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 name="address">
  <option>-请选择你所在的城市-</option>
  <option value="成都">成都</option>
  <option value="攀枝花">攀枝花 </option>
  <option value="南充">南充</option>
  <option value="乐山">乐山</option>
  <option value="泸州">泸州</option>


  <option value="山顶洞">山顶洞</option>
  </select>
  </td>
  </tr>
  <tr>
  <th>请留下你的名字:</th>
  <td>
  <input type="text" name="name" class="textInput"/> 
  </td>
  </tr>
  <tr>
  <th>请留下你的建议:</th>
  <td>
  <textarea name="text" class="textAreaStyle"></textarea>
  </td>
  </tr>
  </tbody>
  </table>
  <input type="submit" name="submit" value="提交" class="submitButton"/>
  </form>
  </body>
下面这是javabean代码:
public class CtrlSql implements CtrlSqlInter{
 public Connection con(){
String DriverName="com.mysql.jdbc.Driver";
String URL="jdbc:mysql://localhost:3306/room?useUnicode=true&characterEncoding=utf-8";
Connection con=null;
try{
Class.forName(DriverName);
con=DriverManager.getConnection(URL,"root","12345");
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();

return con;
 }
 public ResultSet query(String sql){
ResultSet rs=null;
try {
Statement stmt=this.con().createStatement();
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
 }
 public void update(String sql){
try {
Statement stmt=this.con().createStatement();
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
 }
 public static void main(String args[]){
CtrlSql cs=new CtrlSql();
cs.con();
 } 
}我的目的是在页面添加的数据能够添加到mysql数据库里面,然后跳转到message.jsp页面,但是点提交按钮的时候页面就变成什么也没有了,而且数据库里面什么也没有加到!拜托了,老师下午就收了作为期末考试题。大家帮我整一下,跪谢了!!

[解决办法]

Java code
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {   }  }
[解决办法]
同意1楼。
或是把方法名doGet直接改为service
service能将get和post都接收到

热点排行