⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 acceptinsertusertable.jsp

📁 jsp目前成为热门话题,本文件提供了JSP网络编程从基础到实践的源代码.
💻 JSP
字号:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%!
public String codeToString(String str)
{//处理中文字符串的函数
  String s=str;
  try
    {
    byte tempB[]=s.getBytes("ISO-8859-1");
    s=new String(tempB);
    return s;
   }
  catch(Exception e)
   {
    return s;
   }  
}
%>
<%//接收客户端提交的数据
 String username=codeToString(request.getParameter("username"));
 if(username==null)//无内容则设为空串
    username="";
 String password=codeToString(request.getParameter("password"));
 if(password==null)//无内容则设为空串
    password="";
 String usertruename=codeToString(request.getParameter("usertruename"));
 if(usertruename==null)//无内容则设为空串
    usertruename="";
 String age=codeToString(request.getParameter("age"));
 int ageint;
 try
 {
  ageint=Integer.parseInt(age.trim());
 }
 catch(Exception e)
 {
  ageint=0;  //没有输入年龄或输入的年龄不是数字则值为0
 }
 String sex=codeToString(request.getParameter("sex"));
 if(sex==null)//无内容则设为空串
    sex="";
 String address=codeToString(request.getParameter("address"));
 if(address==null)//无内容则设为空串
    address="";
 String telephone=codeToString(request.getParameter("telephone"));
 if(telephone==null)//无内容则设为空串
    telephone="";
%>
<%//构造追加记录SQL语句
 String sqlString=null;//SQL语句
 sqlString="insert into userTable(user_name,user_password,user_true_name,user_age,user_sex,user_address,user_telephone)"+
            " values('"+username+"','"+password+"','"+usertruename+"',"+ageint+",'"+sex+"','"+address+"','"+telephone+"')";
%>
<%//执行SQL语句
 try 
    { Connection con;
      Statement sql;
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
      con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testDatabse","sa","");
      sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
      sql.executeUpdate(sqlString);
      con.close();
    }
 catch(SQLException e1) 
    {
      out.print("SQL异常!");
    }
%>
<head>
<title>用户注册程序</title>
</head>
<body>
<center>
<table border="1" width="700">
  <tr>
    <td width="100%" colspan="2" align="center">用户注册程序</td>
  </tr>
  <tr>
    <td width="100%" colspan="2">追加用户成功!</td>
  </tr>
  </table>
</center>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -