期中考试
1.userbean.java//设置数据
package com.bean;
public class userbean {
private String name;
private String studentid;
private String studenttype;
private String faculty;
private String phone;
private String color;
private String path;
private String other;
public String getName() {
return name;
}
public void setName(String name) {
this.name= name;
}
public String getStudentid() {
return studentid;
}
public void setStudentid(String studentid) {
this.studentid = studentid;
}
public String getStudenttype() {
return studenttype;
}
public void setStudenttype(String studenttype) {
this.studenttype = studenttype;
}
public String getFaculty() {
return faculty; }
public void setFaculty(String faculty) {
this.faculty = faculty;
}
public String getPhone() {
return phone; }
public void setPhone(String phone) {
this.phone = phone;
}
public String getColor() {
return color; }
public void setColor(String color) {
this.color = color;
}
public String getPath() {
return path; }
public void setPath(String path) {
this.path = path;
}
public String getOther() {
return other; }
public void setOther(String other) {
this.other = other;
}
}
2.userdao.java//连接数据库
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.bean.userbean;
import com.util.userutil;
public class userdao {
public void add(userbean claz) throws ClassNotFoundException, SQLException
{
//获得链接对象
Connection connection = userutil.getConnection();
//准备sql语句
String sql = "insert into course(name,studentid,studenttype,faculty,phone,color,path,other) values(?,?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = null;
try {
//创建语句传输对象
preparedStatement = connection.prepareStatement(sql);
// preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, claz.getName());
preparedStatement.setString(2, claz.getStudentid());
preparedStatement.setString(3, claz.getStudenttype());
preparedStatement.setString(4, claz.getFaculty());
preparedStatement.setString(5, claz.getPhone());
preparedStatement.setString(6, claz.getColor());
preparedStatement.setString(7, claz.getPath());
preparedStatement.setString(8, claz.getOther());
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源
//DBUtil.close(preparedStatement);
//DBUtil.close(connection);
}
}
}
3.userutil.java//向数据库里导入数据
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
public class userutil {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Connection connection = null;//连接数据库
Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。
ResultSet rs = null;
//1. 导入驱动jar包
//2.注册驱动
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "lyf123456");
return connection;
}
public static void close(Connection connection ) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4.add.jsp//后端接收数据
<%@page import="com.bean.userbean"%>
<%@page import="com.dao.userdao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
String name = request.getParameter("name");
String studentid = request.getParameter("studentid");
String studenttype = request.getParameter("studenttype");
String faculty = request.getParameter("faculty");
String phone = request.getParameter("phone");
String color = request.getParameter("color");
String path = request.getParameter("path");
String other = request.getParameter("other");
// try{
//if()
{
// throw new ClassException("信息填报错误");
}
// else if()
{
// throw new ClassException("信息填报错误");
}
// else
{
userbean A = new userbean();
A.setName(name);
A.setStudentid(studentid);
A.setStudenttype(studenttype);
A.setFaculty(faculty);
A.setPhone(phone);
A.setColor(color);
A.setPath(path);
A.setOther(other);
userdao Dao = new userdao();
Dao.add(A);
}
%>
填报成功
浙公网安备 33010602011771号