开发工具:
文件大小: 3kb
下载次数: 0
上传时间: 2013-05-12
详细说明: 实现向数据库添加数据。其主要的代码是: <% 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.getP arameter("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"); } %>