扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
明显是路径没找到,你调试下程序,在java代码加下断点,看看有没有进断点。
创新互联是一家专业提供融安企业网站建设,专注与成都做网站、成都网站建设、H5响应式网站、小程序制作等业务。10年已为融安众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
连接数据库的典型步骤
第一步:加载正确的数据库驱动程序
第二步,定义所要连接数据库的地址
String ODBCURL=“jdbc:odbc:dbName”;
String oracleURL= "jdbc:oracle:thin:@host:port:dbName“;
String mysqlURL= "jdbc:mysql:// host:port/dbName“;
String sqlURL=“jdbc:microsoft:sqlserver://host:1433;DatabaseName=dbName”;
第三步,建立与数据库的连接
DriverManager.getConnection(oracleURL,
username,
password);
第四步,创建语句对象
Statement statement = connection.createStatement();
第五步,声明SQL语句,并将该语句通过Statement对象提交给服务器进行执行。
Stringsql= “SELECT * FROM ONTACT_TABLE”;
ResultSet resultSet=statement.executeQuery(sql);
该语句将sql命令提交给数据库服务器进行执行,并将执行结果存储在ResultSet对象中进行执行。
Statement接口主要有如下三个方法:
1. ResultSet executeQuery(String sql)
用于执行查询语句,返回 ResultSet结果集(一个二维表)
2. int executeUpdate(String sql)
用于执行添加、删除或修改操作,返回被更新记录的条数
3. boolean execute(String sql)
执行参数部分的SQL语句;
当SQL语句的执行结果是一个ResultSet结果集时,本方法返回true;并可以通过StatementgetResultSet()方法得到返回的结果集
当SQL语句执行后没有返回的结果集时,该方法返回false
第六步:对查询结果进行分析
while(resultSet.next()){
name =resultSet.getString(1);
phone =resultSet.getString(2);
System.out.println(name+","+phone);
}
第七步,关闭打开的资源
resultSet.close();
statement.close();
connection.close();
将打开的资源关闭。
注意:资源关闭的顺序和打开的顺序恰恰相反。
简单的增删改查功能
package EBookDao;
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 DBUtil.DBUtil;
import DBUtil.EBook;
/*
*查询书集信息
*
* */
public class EBookCZ {
public ListEBook find() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
ListEBook list = new ArrayListEBook();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM ebook";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
EBook eBook = new EBook();
eBook.setId(rs.getInt("ID"));
eBook.setBname(rs.getString("bname"));
eBook.setPrice(rs.getString("price"));
list.add(eBook);
System.out.println(rs.getInt("ID")+rs.getString("bname")+rs.getString("price"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return list;
}
/*
*通过id删除书集信息
*
* */
public static EBook DelById(int id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
EBook book = null;
try {
conn = DBUtil.getConnection();
String sql = "delete ebook ";
sql += "WHERE ID=? ";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
book = new EBook();
book.setId(rs.getInt("ID"));
book.setBname(rs.getString("bname"));
book.setPrice(rs.getString("price"));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return book;
}
}
package EBookServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DBUtil.EBook;
import EBookDao.EBookCZ;
public class EBookServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public EBookServlet() {
super();
}
/**
* Destruction of the servlet. br
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. br
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. br
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String method = request.getParameter("method");
String url = "";
EBookCZ bookCZ=new EBookCZ();
if (method != null method.equals("find")) {
System.out.println("find做完了");
url = "EBookMNG.jsp";
ListEBook list = bookCZ.find();
request.setAttribute("list", list);
} else if (method != null method.equals("findById")) {
System.out.println("findbyid做完了");
url = "OK.jsp";
int id = Integer.parseInt(request.getParameter("id"));
EBook book = EBookCZ.DelById(id);
request.setAttribute("book", book);
}
request.getRequestDispatcher(url).forward(request, response);
out.flush();
out.close();
}
/**
* Initialization of the servlet. br
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流