学习就是一点一点的积累 方法很多,主要记个模板,阅读其他代码 可以了解其它更合适自己的。代码千篇一律,目的始终如一。加油吧小白,在下也是不断学习的小白。通往大神之路还很遥远。
添加数据先插寻 如果id存在 添加失败
package com.Dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBUtil {
static Connection conn = null;
static Statement stat = null;
static PreparedStatement pstm = null;
static ResultSet rs = null;
//通用的 增, 删 ,改 ,操作
public static int executeUpdate(String sql, Object... params) {
int rows = 0;
try {
conn = getConnection();//静态方法是直接调用驱动加载
pstm = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pstm.setObject(i + 1, params[i]);
}
rows = pstm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeALL(rs, stat, conn);
}
return rows;
}
public static Connection getConnection() throws SQLException {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载数据驱动
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "scott";
String password = "tiger";
conn = DriverManager.getConnection(url, user, password);// 连接数据库
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭的方法
public static void closeALL(ResultSet rs, Statement stat, Connection conn) {
try {
if (rs != null) {
rs.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.Dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entry.School;
public class SchoolDao {
Connection conn =null;
PreparedStatement pastm =null;
ResultSet Query =null;
public boolean select(int id) {
return queySchool(id)==null?false:true;
}
//查询
public School queySchool(int id) {
School school=null;
try {
int ros =-1;
conn = DBUtil.getConnection();
String sql ="SELECT * FROM School WHERE id =?";
pastm = conn.prepareStatement(sql);
pastm.setInt(1, id);
Query = pastm.executeQuery();
while(Query.next()) {
int id1 = Query.getInt("id");
String name = Query.getString("name");
String DEPARTMENT = Query.getString("DEPARTMENT");
String sex = Query.getString("sex");
String DORMITORY = Query.getString("DORMITORY");
school = new School(id1, name, DEPARTMENT, sex, DORMITORY);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.closeALL(Query, pastm, conn);
}
return school;
}
//添加
public boolean SelectSchool(School school) {
try {
int date =0;
conn = DBUtil.getConnection();
String sql ="INSERT INTO School VALUES(?,?,?,?)";
date = pastm.executeUpdate();
pastm.setInt(1, school.getId());
pastm.setString(2, school.getName());
pastm.setString(3, school.getDEPARTMENT());
pastm.setString(4, school.getSex());
pastm.setString(5, school.getDORMITORY());
pastm = conn.prepareStatement(sql);
if(date>0) {
return true;
}else {
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
DBUtil.closeALL(Query, pastm, conn);
}
}
}
package com.entry;
public class School {
private int id;
private String name;
private String DEPARTMENT;
private String sex;
private String DORMITORY;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDEPARTMENT() {
return DEPARTMENT;
}
public void setDEPARTMENT(String dEPARTMENT) {
DEPARTMENT = dEPARTMENT;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDORMITORY() {
return DORMITORY;
}
public void setDORMITORY(String dORMITORY) {
DORMITORY = dORMITORY;
}
public School(int id, String name, String dEPARTMENT, String sex, String dORMITORY) {
super();
this.id = id;
this.name = name;
DEPARTMENT = dEPARTMENT;
this.sex = sex;
DORMITORY = dORMITORY;
}
@Override
public String toString() {
return "School [id=" + id + ", name=" + name + ", DEPARTMENT=" + DEPARTMENT + ", sex=" + sex + ", DORMITORY="
+ DORMITORY + "]";
}
}
package com.service;
import com.Dao.SchoolDao;
import com.entry.School;
public class SchoolService {
SchoolDao schoolDao = new SchoolDao();
public boolean addSchool(School school) {
if(!schoolDao.select(school.getId())) {
return true;
}else {
return false;
}
}
}
package com.servite;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.entry.School;
import com.service.SchoolService;
public class addServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF-8");
int id =Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String DEPARTMENT = request.getParameter("DEPARTMENT");
String sex = request.getParameter("sex");
String DORMITORY = request.getParameter("DORMITORY");
School school = new School(id, name, DEPARTMENT, sex, DORMITORY);
SchoolService s = new SchoolService();
boolean re = s.addSchool(school);
PrintWriter out = response.getWriter();
if(re) {
out.println("添加成功");
}else {
out.println("添加失败");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
<form action="addServlet"method="post"><br>
<table>
<tr><td>学号:<input type="text" name="id"></td></tr>
<tr><td>姓名:<input type="text" name="name"></td></tr>
<tr><td>院系:<input type="text" name="DEPARTMENT"></td></tr>
<tr><td>性别:<input type="text" name="sex"></td></tr>
<tr><td>宿舍:<input type="text" name="DORMITORY"></td></tr>
<tr><td> <input type="submit" value="提交">
<input type="reset" value="重置"></td></tr>
</table>
<servlet>
<servlet-name>addServlet</servlet-name>
<servlet-class>com.servite.addServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addServlet</servlet-name>
<url-pattern>/addServlet</url-pattern>
</servlet-mapping>