重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
连接数据库的典型步骤
公司主营业务:网站制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出洞头免费做网站回馈大家。
第一步:加载正确的数据库驱动程序
第二步,定义所要连接数据库的地址
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();
将打开的资源关闭。
注意:资源关闭的顺序和打开的顺序恰恰相反。
public class ShoppingCart {//购物车类
private Producted prod;//商品类,存放商品信息
private int num;//商品数量
public Producted getProd() {
return prod;
}
public void setProd(Producted prod) {
this.prod = prod;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
public class Producted {//商品类
private String prodName;//商品名称
private Double marketPrice;//市场价
private Double sellPrice;//本店价
private Double save;//优惠
private Short points;//积分
public String getProdName() {
return prodName;
}
public void setProdName(String prodName) {
this.prodName = prodName;
}
public Double getMarketPrice() {
return marketPrice;
}
public void setMarketPrice(Double marketPrice) {
this.marketPrice = marketPrice;
}
public Double getSellPrice() {
return sellPrice;
}
public void setSellPrice(Double sellPrice) {
this.sellPrice = sellPrice;
}
public Double getSave() {
return save;
}
public void setSave(Double save) {
this.save = save;
}
public Short getPoints() {
return points;
}
public void setPoints(Short points) {
this.points = points;
}
}
有用请采纳
悬赏什么的不值钱的,没人愿意花时间做这个的,推荐CSDN网站自己找找类似的,然后改改,如果改的能力都没有的话,那也就没办法了
简单的增删改查功能
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
}
}