第一次尝试连接数据库
首先使用Java web 做一个简单的页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <form action="UserServlet" method="post"> <table border="0"> <tr> <td>登录账号:</td> <td><input type="text" name="user"/></td> </tr> <tr> <td>登录密码:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td>姓别:</td> <td><select name="sex"> <option>男</option> <option>女</option> </select></td> </tr> <tr> <td>性名:</td> <td><input type="text" name="name"/></td> </tr> <tr> <td>学号:</td> <td><input type="text" name="num"/></td> </tr> <tr> <td>电子邮件:</td> <td><input type="text" name="mail"/></td> </tr> <tr> <td>所在学院:</td> <td><input type="text" name="xueyuan"/></td> </tr> <tr> <td>所在系:</td> <td><input type="text" name="zhuanye"/></td> </tr> <tr> <td>所在班级:</td> <td><input type="text" name="cl"/></td> </tr> <tr> <td>入学年份(届):</td> <td><select name="year"> <option>2015</option> <option>2016</option> <option>2017</option> <option>2018</option> <option>2019</option> <option>2020</option> </select></td> </tr> <tr> <td>生源地:</td> <td><input type="text" name="home"/></td> </tr> <tr> <td>备注:</td> <td><input type="text" name="beizhu"/></td> </tr> <tr> <td><input type="submit" value="提交"/></td> </tr> </table> </form> </body> </html>
打开数据库的代码
package util; import java.sql.*; public class DBUtil { String url = "jdbc:mysql://localhost:3306/studentinfo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false"; String user = "root"; String password = "20183641"; private java.sql.Connection conn = null; private Statement stmt = null; public DBUtil() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); } catch (Exception ex) { System.out.println("数据库连接失败!"); } } /** * 执行更新操作 * * @param s * SQL语句 * @return 更新操作的结果 */ public int executeUpdate(String s) { int result = 0; try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.out.println("更新出现异常!"); } return result; } /** * 执行查询操作 * * @param s * SQL语句 * @return 查询结果 */ public ResultSet executeQuery(String s) { ResultSet rs = null; try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.out.println("查询出现异常!"); } return rs; } /** * 关闭数据库 */ public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
将数据存入数据库的各个层
package servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import dao.InfoDao; /** * Servlet implementation class UserServlet */ @WebServlet("/UserServlet") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UserServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String user=request.getParameter("user"); String password=request.getParameter("password"); String sex=request.getParameter("sex"); String name=request.getParameter("name"); String num=request.getParameter("num"); String mail=request.getParameter("mail"); String xueyuan=request.getParameter("xueyuan"); String zhuanye=request.getParameter("zhuanye"); String cl=request.getParameter("cl"); String year=request.getParameter("year"); String home=request.getParameter("home"); String beizhu=request.getParameter("beizhu"); int r=InfoDao.add(user, password, sex, name, num, mail, xueyuan, zhuanye, cl, year, home, beizhu); if(r==1) { request.getRequestDispatcher("NewFile.html").forward(request, response); }else { request.getRequestDispatcher("NewFile.jsp").forward(request, response); } } }
package dao; import bean.Info; import util.DBUtil; public class InfoDao { public static int add(String user,String password,String sex,String name,String num,String mail,String xueyuan,String zhuanye,String cl,String year,String home,String beizhu) { Info a=new Info(); int result=0; String sql="insert into userinfo(user,password,sex,name,num,mail,xueyuan,zhuanye,cl,year,home,beizhu)values('"+user+"','"+password+"','"+sex+"','"+name+"','"+num+"','"+mail+"','"+xueyuan+"','"+zhuanye+"','"+cl+"','"+year+"','"+home+"','"+beizhu+"')"; //String sql="insert into info values(null,'"+user+"','"+password+"','"+sex+"','"+name+"','"+num+"','"+mail+"','"+xueyuan+"','"+zhuanye+"','"+cl+"','"+year+"','"+home+"','"+beizhu+"')"; //System.out.println(sql); //String sql="insert into userinfo values(null,"+user+"','"+password+"','"+name+"','"+sex+"','"+diqu+"','"+phone+"','"+eml+")"; DBUtil b=new DBUtil(); result=b.executeUpdate(sql); return result; } }
package bean; public class Info { private int id; private String user; private String password; private String sex; private String name; private String num; private String mail; private String xueyuan; private String zhuanye; private String cl; private String year; private String home; private String beizhu; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getXueyuan() { return xueyuan; } public void setXueyuan(String xueyuan) { this.xueyuan = xueyuan; } public String getZhuanye() { return zhuanye; } public void setZhuanye(String zhuanye) { this.zhuanye = zhuanye; } public String getCl() { return cl; } public void setCl(String cl) { this.cl = cl; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } }