使用表单作为输入并将该数据保存到2个表中

时间:2014-04-16 15:29:03

标签: mysql sql jsp

我正在研究我的项目jsp和mysql,我创建了一个页面添加学生,所以只有一个表学生一切正常。但是当我尝试将输入数据存储添加到2个表中时会出现问题。下面我详细解释。

我有两张桌子。

学生{rollno,sname,sem,branch,section}

用户{uid,pass}

当我填写addstudent页面的详细信息时,我希望用户表中存储在uid中的学生的rollno我尝试但我的数据只保存到学生表中。??

这是我的学生注册码。

<form>
<table cellspacing="0" cellpadding="0" border="1" width="55%">
  <tr>
    <th width="50%" colspan="2">
      <strong><font size="5">
          <font face="Century">
            REGESTRATION
          </font>
        </font></strong>
      &nbsp;
    </th>
  </tr>
  <tr>
    <td width="32%">
      <strong>ROLL NUMBER</strong>
      &nbsp;</td>
    <td width="68%">
        <input type="text" name="roll no" maxlength="12" size="15"/>
      </td>
  </tr>
  <tr>
    <td width="32%" height="20">
      <strong>NAME</strong>
      &nbsp;</td>
    <td width="68%" height="20">
        <input type="text" name="name" maxlength="50" size="30"/>
      </td>
  </tr>
   <tr>
    <td width="32%">
      <strong>BRANCH</strong>
      &nbsp;</td>
    <td width="68%">
        <select size="1" name="branch">
            <option value="CS" selected>COMPUTER SCIENCE</option>
          <option value="EC">ELECTRONICS & COMMUNICATION</option>
          <option value="ME">MECHANICAL</option>
          <option value="CE">CIVIL</option>
          <option value="IT">INFORMATION & TECHNOLOGY</option>

        </select>
      </td>
  </tr>

  <tr>
    <td width="32%">
      <strong>SEMISTER</strong>
      &nbsp;</td>
    <td width="68%">
        <select size="1" name="sem">
            <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
        </select>
      </td>
  </tr>
  <tr>
    <td width="32%">
      <strong>SECTION</strong>
      &nbsp;</td>
    <td width="68%">
        <select size="1" name="sec">
            <option value="A" selected>A</option>
          <option value="B">B</option>
          <option value="C">C</option>
          <option value="D">D</option>

        </select>
      </td>
  </tr>
  <tr>
    <td width="50%" colspan="2">
         <input type="submit" name="b1" value="Submit"/>
      </td>
  </tr>
</table>

 <% 

           if (request.getParameter("b1")!= null)
    {
        String sql = "insert into stu_reg values('"+
                  request.getParameter("roll no")+"','"+
                  request.getParameter("name")+"','password','"+
                  request.getParameter("sem")+"','"+
                  request.getParameter("sec")+"','"+
                  request.getParameter("branch")+"')";
        DBConnect obj=new DBConnect();
        obj.Query_Executer(sql);
       // out.println("<b>Registration Successful</b>");
        response.sendRedirect("StuReg.jsp");
    }
 %>
 </form>

继承人我的bd连接连接代码:

package DBUtil;

import java.sql.*;

public class DBConnect {
  Connection conn;

public DBConnect()
{
    try
    {
      Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/Feedback";
        conn = DriverManager.getConnection(url,"root", "");
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
}

public void Query_Executer(String sql)
{
    try
    {
        Statement stat = conn.createStatement();
        stat.execute(sql);
        stat.close();
        //System.out.println("Query executed successfully");

    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
}

public ResultSet Query_Returner(String sql)
{
    ResultSet rs=null;
    try
    {
        Statement stat = conn.createStatement();
        rs = stat.executeQuery(sql);
    }
    catch(Exception ex)
    {
        System.out.println(ex);
    }
    return rs;

}

}

任何有用的帮助。

由于

0 个答案:

没有答案