- 博客(1)
- 资源 (2)
- 收藏
- 关注
JSP+java开发
实现向数据库添加数据。其主要的代码是:
<body style=color:blue>
<%
boolean flag = false;
Login ck2 = new Login();
request.setCharacterEncoding("UTF-8");
String num = request.getParameter("num");
String password = request.getParameter("password");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String tel = request.getParameter("num");
String dept = request.getParameter("dept");
String address = request.getParameter("address");
flag = ck2.Logincheck(num, password);
System.out.println(flag);
if (!flag) {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 驱动类
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=users"; // 连接数据库的地址
String user = "sa";// 数据库用户名
String pwd = "123";// 数据库密码
Connection ct = null;
PreparedStatement ps = null;
ResultSet rs = null;
Class.forName(driver);// 加载驱动程序,实例化
ct = DriverManager.getConnection(url, user, pwd);// 创建一个连接
//sql=insert into admins('num','name','sex','age','tel','password')
String sql = "insert into admins(num,password,name,sex,age,tel,dept,address)"
+ "values('"
+ num
+ "','"
+ password
+ "','"
+ name
+ "','"
+ sex
+ "','"
+ age
+ "','"
+ tel
+ "','"
+ dept + "','" + address + "' )";
System.out.println(sql);
ps = ct.prepareStatement(sql);
ps.execute();
ct.close();
response.sendRedirect("denglu.jsp");
} else {
System.out.print("注册失败 ");
response.sendRedirect("zhuce.jsp");
}
%>
</body>
</html>
2013-05-12
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人