标签:cut 数据库 提交 erro java body 链接 for values
一:设计思想
链接数据库的步骤就是:
* 1 加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
String classname = "root";
String classteacher = "root";
String classspace = "jdbc:mysql://localhost:3306/class";
Connection connection = null;
* 2 创建链接对象
connection = DriverManager.getConnection(classspace,classname,classteacher);
* 3 创建语句传输对象
ResultSet resultSet=null;
PreparedStatement preparedStatement = null;
Connection connection = DBUtil.getConnection();
* 4 接收结果集对象
resultSet = preparedStatement.executeQuery();
* 5 遍历
sql = "insert into t_user(username,password,nickname) value (?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getNickname());
preparedStatement.executeUpdate();
* 6 关闭资源
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
连接上数据以后就是要写jsp文件,在jsp文件中要进行输入的写入,在进行提交到数据库就好了
二:实验代码:
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; } }
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(); } } }
<%@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(); System.out.println(classname); st.setClassname(classname); st.setClassteacher(classteacher); st.setClassspace(classspace); shujuku s = new shujuku(); try{ s.gets(st); } catch(NullPointerException q) { q.printStackTrace(); } %> </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> <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>
三:运行结果
个人程序总结
标签:cut 数据库 提交 erro java body 链接 for values
原文地址:http://www.cnblogs.com/zhaochunhui/p/7911802.html