java数据库中这样实现增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java数据库中这样实现增删改查相关的知识,希望对你有一定的参考价值。
要快速的!!!!!!!!!
//操作acess的package cn.zhtech;
import java.sql.*;
import java.io.*;
public class DBManager
/**
* @param args
*/
public static void main(String[] args)
// TODO 自动生成方法存根
String strPath="";//当前程序根路径
try
File f=new File(".");
strPath=f.getCanonicalPath();
catch(IOException e)
System.out.println(e.toString());
//access文件路径
String url="jdbc:odbc:Driver=MicroSoft Access Driver (*.mdb);DBQ="+strPath+"\\data\\aa.mdb";
String username="";
String password="";
Connection con;
Statement stml;
ResultSet res;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//加载驱动
con=DriverManager.getConnection(url, username, password);//获取连接
stml=con.createStatement();//建立statement
res=stml.executeQuery("select * from test");//查询
while(res.next())//显示
System.out.println(res.getString("u_name")+"\n");
res.close();
stml.executeUpdate("insert into test(u_name) values('kkk')");//插入
stml.executeUpdate("delete from test where u_ID=3");//删除
stml.executeUpdate("update test set u_name='mengkaide' where u_ID=4");//修改
stml.close();//关闭
con.close();
catch(Exception e)
System.out.println(e.toString());
参考技术A 提取单条记录
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
catch(Exception e)
e.printStackTrace();
finally
try
try
stmt.close();
con.close();
catch (SQLException e)
e.printStackTrace();
catch (Exception e)
e.printStackTrace();
参考技术B 一分都不给啊,大哥!
java MVC 实现增删改查
public class AddServlet extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String description = req.getParameter("description");
String baseprice = req.getParameter("baseprice");
String writer = req.getParameter("writer");
String publish = req.getParameter("publish");
String pages = req.getParameter("pages");
String images = req.getParameter("images");
String stock = req.getParameter("stock");
Connection conn=null;
PreparedStatement pstm=null;
try
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/ecport";
conn = DriverManager.getConnection(url, "root", "");
String sql = "insert into product values(0,?,?,?,?,?,?,?,?)";
pstm = conn.prepareStatement(sql);
pstm.setObject(1, name);
pstm.setObject(2, description);
pstm.setObject(3, baseprice);
pstm.setObject(4, writer);
pstm.setObject(5, publish);
pstm.setObject(6, pages);
pstm.setObject(7, images);
pstm.setObject(8, stock);
//执行sql
int result = pstm.executeUpdate();
//3.把处理的结果响应给用户(响应对象)
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
if(result == 1)
out.println("<html><body><h3 style='color:red'>恭喜你,新增图书成功!!!</h3></body></html>");
else
out.println("<html>新增失败</html>");
out.close();
catch(Exception e)
e.printStackTrace();
finally
try
pstm.close();
conn.close();
catch (SQLException e)
e.printStackTrace();
求大神帮忙把 上面的增加功能改成MVC框架实现
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddServlet extends HttpServlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String description = req.getParameter("description");
String baseprice = req.getParameter("baseprice");
String writer = req.getParameter("writer");
String publish = req.getParameter("publish");
String pages = req.getParameter("pages");
String images = req.getParameter("images");
String stock = req.getParameter("stock");
Product p=new Product();
ProductDao Dao = new ProductDao();
p.setName(name);
p.setDescription(description);
p.setBaseprice(baseprice);
p.setWriter(writer);
p.setPublish(publish);
p.setPages(pages);
p.setImages(images);
p.setStock(stock);
int result = 0;
try
result = Dao.save(p);
catch (Exception e)
e.printStackTrace();
resp.setContentType("text/html");
if(result == 1)
resp.sendRedirect("ok.jsp");
else
resp.sendRedirect("error.jsp");
ProductDao类实现了使用对象想数据库中添加记录的功能;
实现了MVC中的M模型
package servlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class ProductDao
public int save(Product p) throws Exception
Connection conn = null;
PreparedStatement prep = null;
try
conn = DBUtil.getConnection();
String sql = "insert into product values(0,?,?,?,?,?,?,?,?)";
prep = conn.prepareStatement(sql);
prep.setObject(1, p.getName());
prep.setObject(2, p.getDescription());
prep.setObject(3, p.getBaseprice());
prep.setObject(4, p.getWriter());
prep.setObject(5, p.getPublish());
prep.setObject(6, p.getPages());
prep.setObject(7, p.getImages());
prep.setObject(8, p.getStock());
int num=prep.executeUpdate();
return num;
catch (Exception e1)
e1.printStackTrace();
throw e1;
finally
DBUtil.close(conn);
Product类根据数据库的字段声明类的属性根据表名命名类名;
package servlet;
public class Product
private Object name;
private Object description;
private Object baseprice;
private Object writer;
private Object publish;
private Object pages;
private Object images;
private Object stock;
public Object getName()
return name;
public void setName(Object name)
this.name = name;
public Object getDescription()
return description;
public void setDescription(Object description)
this.description = description;
public Object getBaseprice()
return baseprice;
public void setBaseprice(Object baseprice)
this.baseprice = baseprice;
public Object getWriter()
return writer;
public void setWriter(Object writer)
this.writer = writer;
public Object getPublish()
return publish;
public void setPublish(Object publish)
this.publish = publish;
public Object getPages()
return pages;
public void setPages(Object pages)
this.pages = pages;
public Object getImages()
return images;
public void setImages(Object images)
this.images = images;
public Object getStock()
return stock;
public void setStock(Object stock)
this.stock = stock;
DBUtil类封装的是对数据库的连接:
package servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* JDBC工具类:
* 提供了获得连接,关闭连接的相关的方法。
*
*/
public class DBUtil
private static String url="jdbc:mysql://localhost:3306/ecport";
public static Connection getConnection() throws Exception
Connection conn = null;
try
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.
getConnection(url,"root", "");
catch (Exception e)
e.printStackTrace();
throw e;
return conn;
public static void close(Connection conn)
if(conn != null)
try
conn.close();
catch (SQLException e)
e.printStackTrace();
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
Connection conn =
getConnection();
System.out.println(conn);
最后是MVC中的视图,实现的shiMVC中的V视图,在AddServlet中根据不同的结果受用重定向跳转到指定的页面中,表明是否添加成功。
成功页面:
<%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %>
<html>
<body>
<h3 style='color:red'>恭喜你,新增图书成功!!!</h3>
</body>
</html>
失败页面:
<%@page pageEncoding="utf-8"
contentType="text/html;charset=utf-8" %>
<html>新增失败</html>
以上是关于java数据库中这样实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章