今天java课上要写的一个课堂作业是要链接数据库从而完成增这项功能
首先是链接到数据库
package com.con; import java.sql.Connection; import java.sql.DriverManager; public class Conn { public Connection getCon() { try { Class.forName("com.mysql.cj.jdbc.Driver"); String url="jdbc:mysql://localhost/test1?serverTimezone=UTC"; String user="root"; String password="123456"; Connection conn=DriverManager.getConnection(url,user,password); System.out.println(conn.getMetaData().getURL()); return conn; }catch(Exception e) { e.printStackTrace(); return null; } } }
然后是定义变量然后编写函数
package com.mode; public class Test { private int id; private String username; private String password; private byte gender; private String name; private int number; private String email; private String school; private String department; private String team; private String year; private String address; private String remarks; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public byte getGender() { return gender; } public void setGender(byte gender) { this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getTeam() { return team; } public void setTeam(String team) { this.team = team; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getRemarks() { return remarks; } public void setRemarks(String remarks) { this.remarks = remarks; } }
在之后就是写函数读入值
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import com.model.Test; public class Userservice { private Connection conn; private PreparedStatement pstmt; public Userservice() { conn=new com.conn.Conn().getCon(); } public boolean addUser(Test user) { try { pstmt=conn.prepareStatement("insert into test1"+"(username,password,gender,name,number,email,school,department,team,year,address,remarks)"+"values(?,?,?,?,?,?,?,?,?,?,?,?)"); pstmt.setString(1,user.getName()); pstmt.setString(2,user.getPassword()); pstmt.setByte(3,user.getGender()); pstmt.setString(4,user.getName()); pstmt.setInt(5,user.getNumber()); pstmt.setString(6,user.getEmail()); pstmt.setString(7,user.getSchool()); pstmt.setString(8,user.getDepartment()); pstmt.setString(9,user.getTeam()); pstmt.setString(10,user.getYear()); pstmt.setString(11,user.getAddress()); pstmt.setString(12,user.getRemarks()); pstmt.executeUpdate(); return true; }catch(SQLException e) { e.printStackTrace(); return false; } } }
最后是调用
package com.test; import com.conn.Conn; public class Test { public static void main(String[] args) { new Conn().getCon(); } }
然后是前端的操作
第一个是显示成功与否
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "- //W3C..DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>20173502</title> </head> <body> <%request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="user" class="com.model.Test"></jsp:useBean> <jsp:useBean id="userservice" class="com.service.Userservice"></jsp:useBean> <jsp:setProperty property="*" name="user"/> <% if(userservice.addUser(user)) out.print("success"); else out.print("failure"); %> </body> </html>
第二个是界面设计
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "- //W3C..DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>20173502</title> <script type="text/javascript"> founction check(){ var d=document.getElementById("password").value; if(d.length<9) alert("密码长度必须8位以上!"); } </script> </head> <body> <form action="NewFile_result.jsp" > <table align="center"> <tr> <td>登录账号</td> <td><input type="text" name="username" pattern="[w]{6,12}"></td> </tr> <tr> <td>登录密码</td> <td><input type="password" name="password" id="password" pattern="[a-zA-Z0-9_-]{8}"></td> </tr> <tr> <td align="right">性别</td> <td><input type="radio" name="gender" checked value="1">男 <input type="radio" name="xb" value="0">女</td> </tr> <tr> <td align="right">姓名</td> <td><input type="text" name="name"></td> </tr> <tr> <td align="right">学号</td> <td><input type="text" name="number" pattern="2018+[0-9]{4}"></td> </tr> <tr> <td>电子邮箱</td> <td><input type="text" name="email" pattern="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$"></td> </tr> <tr> <td>所在学校</td> <td><input type="text" name="school"></td> </tr> <tr> <td align="right">所在系</td> <td><input type="text" name="department"></td> </tr> <tr> <td>所在班级</td> <td><input type="text" name="team"></td> </tr> <tr> <td>入学年份</td> <td><select name="year"> <option>2016</option> <option>2017</option> <option>2018</option> <option>2019</option> </select> </td> </tr> <tr> <td>生源地</td> <td><input type="text" name="address"><td> </tr> <tr> <td>备注</td> <td><textarea rows="3" cols="20" name="remarks"></textarea><td> </tr> <tr> <td></td> <td><input type="submit" value="提交"><td> </tr> </table> </form> </body> </html>