教室添加信息
实验源代码:
package com.jaovo.msg.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
public static Connection getConnection() {
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String classname = "root";
String classteacher = "root";
String classspace = "jdbc:mysql://localhost:3306/class";
Connection connection = null;
try {
//2 创建链接对象connection
connection = DriverManager.getConnection(classspace,classname,classteacher);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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();
}
}
}
package com.jaovo.msg.teach;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import sun.net.www.content.text.plain;
public class shujuku {
public void gets(student st) {
//获得链接对象
ResultSet resultSet=null;
PreparedStatement preparedStatement = null;
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "insert into t_class(classname,classteacher,classspace) values(?,?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
preparedStatement.setString(1, st.getClassname());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
preparedStatement.setString(2, st.getClassteacher());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
preparedStatement.setString(3, st.getClassspace());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
}
package com.jaovo.msg.teach;
public class student {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String classteacher;
private String classname;
private String classspace;
public String getClassteacher() {
return classteacher;
}
public void setClassteacher(String classteacher) {
this.classteacher = classteacher;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public String getClassspace() {
return classspace;
}
public void setClassspace(String classspace) {
this.classspace = classspace;
}
}
<%@ 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>
<title>信息添加页面</title>
</head>
<body>
<%=request.getAttribute("error") %>
<form action="add.jsp" method="get">
<table align="center" border="1" width="500">
<tr>
<td> 课程名称 </td>
<td>
<input type="text" name="classname" />
</td>
</tr>
<tr>
<td>课程教师</td>
<td>
<input type="text" name="classteacher" />
</td>
</tr>
<tr>
<td>上课教室</td>
<td>
<input type="text" name="classspace" />
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="保存" />
</td>
</tr>
</table>
</form>
</body>
</html>
<%@page import="com.jaovo.msg.teach.shujuku"%>
<%@page import="com.jaovo.msg.teach.student"%>
<%@ 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>
<%
//接收客户端传递过来的参数
String classname = request.getParameter("classname");
if(classname==null||classname=="")
{
System.out.println("课程名字不得为空");
}
String classteacher = request.getParameter("classteacher");
if(classteacher==null||classteacher=="")
{
System.out.println("课程老师不得为空");
}
String classspace = request.getParameter("classspace");
if(classspace==null||classspace=="")
{
System.out.println("课程教室不得为空");
}
student st=new student();
st.setClassname(classname);
st.setClassteacher(classteacher);
st.setClassspace(classspace);
shujuku s = new shujuku();
try{
s.gets(st);
}
catch(NullPointerException q)
{
q.printStackTrace();
}
%>
</html>
实验截图:


浙公网安备 33010602011771号