详细解析java JDBC实现增删改查
Posted 小明同学`
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了详细解析java JDBC实现增删改查相关的知识,希望对你有一定的参考价值。
目录
实现java数据库的增删改查基本步骤
1, jdbc类数据库链接和驱动导入,test类测试增删改查。
2,Book类编写实体类(与数据库mysql的属性一至)
3,BookDaoompl接口编写增删改查抽象方法
4,BookDao实现BookDaoompl接口中 的抽象方法
包与类的结构。
一,与数据库建立连接,驱动包导入。
package Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class jdbc
static
try
Class.forName("com.mysql.jdbc.Driver");//驱动加载
catch (ClassNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
public static Connection con() throws SQLException
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
Connection con = DriverManager.getConnection(url, "root", "sa");//与数据建立连接
return con;
public static void main(String[] args)
try
Connection con = jdbc.con();
System.out.println(con);//测试链接是否成功
catch (SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
连接测试成功显示地址;
二,创建实体类(与数据库的属性相一致)
package Entity;
public class Book
private int id;
private String name;
private int age;
private String sex;
private String dizhi;//设置私有属性
//get和set得到和设置属性值
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 int getAge()
return age;
public void setAge(int age)
this.age = age;
public String getSex()
return sex;
public void setSex(String sex)
this.sex = sex;
public String getDizhi()
return dizhi;
public void setDizhi(String dizhi)
this.dizhi = dizhi;
//建立有参和无参的构造方法
public Book(int id, String name, int age, String sex, String dizhi)
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.dizhi = dizhi;
public Book()
super();
//建立tostring方法返回string值
@Override
public String toString()
return "Book [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", dizhi=" + dizhi + "]";
三,创建接口
package Dao;
import java.sql.SQLException;
import java.util.List;
import Entity.Book;
public interface BookDaoimpl
int insertbook(Book book) throws SQLException;//增
int delete(int id) throws SQLException;//删
int update(Book book) throws SQLException;//改
List<Book> selectBook() throws SQLException;//查
四,sql语句预编译和执行代码
package Dao;
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 Entity.Book;
import Util.jdbc;
public class BookDao implements BookDaoimpl
@Override
//增
public int insertbook(Book book) throws SQLException
// TODO Auto-generated method stub
Connection con = jdbc.con();//连接数据库
//sql语句的预编译防止sql注入 ,?代表占位符
//表名和属性要与数据库表明和属性一至
String sql = "insert into book (name,age,sex,dizhi) values(?,?,?,?)";
//创建PreparedStatement对象用来执行sql语句
PreparedStatement per = con.prepareStatement(sql);
//添加和得到各个属性的值
per.setString(1, book.getName());
per.setInt(2, book.getAge());
per.setString(3, book.getSex());
per.setString(4, book.getDizhi());
//执行sql语句,executeUpdate()获取影响的行数并返回
int rows = per.executeUpdate();
//返回执行影响的行数
return rows;
//删
@Override
public int delete(int id) throws SQLException
// TODO Auto-generated method stub
//连接数据库
Connection con = jdbc.con();
//删除sql语句预编译
String sql = "delete from book where id=?";
PreparedStatement per = con.prepareStatement(sql);
//获取删除的id
per.setInt(1, id);
//执行删除sql语句
int wors = per.executeUpdate();
//返回影响行数,executeUpdate()获取影响的行数并返回
return wors;
//改
@Override
public int update(Book book) throws SQLException
//获取连接
Connection con = jdbc.con();
// TODO Auto-generated method stub
//sql语句编译
String sql = "update book set name=?,age=?,sex=?,dizhi=?where id=?";
PreparedStatement per = con.prepareStatement(sql);
//得到修改的数据
per.setString(1, book.getName());
per.setInt(2, book.getAge());
per.setString(3, book.getSex());
per.setString(4, book.getDizhi());
per.setInt(5, book.getId());
//执行修改sql语句,executeUpdate()获取影响的行数并返回
int wors = per.executeUpdate();
//返回影响行数
return wors;
//查
@Override
public List<Book> selectBook() throws SQLException
// TODO Auto-generated method stub
//获取连接
Connection con = jdbc.con();
//查询语句编译
String sql = "select * from book";
PreparedStatement per = con.prepareStatement(sql);
//executeQuery()方法查询到数据库的相应结果存放到ResultSet
ResultSet rest = per.executeQuery();
//建立list集合存放输出查询结果
List<Book> book = new ArrayList<Book>();
//rest.next()方法将指针下移直到没下一行返回FALSE。
while (rest.next())
//将查询结果依次储存
int id = rest.getInt("id");
String name = rest.getString("name");
int age = rest.getInt("age");
String sex = rest.getString("sex");
String dizhi = rest.getString("dizhi");
//建立一个新对象放置结果
Book book1 = new Book(id, name, age, sex, dizhi);
//将结果增加到list集合中
book.add(book1);
返回查询结果
return book;
增删改与查的执行语句的方法不一致,因为增删改只需要返回执行中影响的正确行数,so我们只要使用executeUpdate()方法,而查询需要返回查询的结果集要用executeQuery()方法进行储存
五,test测试类
package Test;
import java.sql.SQLException;
import java.util.List;
import Dao.BookDao;
import Dao.BookDaoimpl;
import Entity.Book;
public class test
public static void main(String[] args) throws SQLException
// TODO Auto-generated method stub
BookDaoimpl dao = new BookDao();
Book book = new Book();
List<Book> rows = dao.selectBook();
System.out.println(rows);
查询结果
java 程序 增删改查
在java beans 像图中的写增删改查? 向student 表中增删改查,,, GUI的 帮忙每个按钮有一个事件驱动
参考技术A class NewAction implements ActionListenerString url="jdbc:odbc:s";
String user="sa";
String passwd="";
public void actionPerformed(ActionEvent e)
// TODO Auto-generated method stub
if(e.getSource()==jb)
Find();
else if(e.getSource()==jb1)
Write();
JOptionPane.showMessageDialog(null, "添加成功");
else if(e.getSource()==jb3)
Del();
JOptionPane.showMessageDialog(null, "删除成功");
else if(e.getSource()==jb4)
Update();
JOptionPane.showMessageDialog(null, "修改成功");
else if(e.getSource()==jb5)
Build();
public void Find()
boolean f=false;
String lk="select * from std";
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery(lk);
while(rs.next())
String p=rs.getString(1);
f=(jf13.getText().trim()).equals(p.trim());
if(f)
jf1.setText(p);
jf1.setEditable(false);
jf2.setText(rs.getString(2));
jf2.setEditable(false);
jf3.setText(rs.getString(3));
jf3.setEditable(false);
jf4.setText(rs.getString(4));
jf4.setEditable(false);
jf5.setText(rs.getString(5));
jf5.setEditable(false);
jf6.setText(rs.getString(6));
jf6.setEditable(false);
jf7.setText(rs.getString(7));
jf7.setEditable(false);
jf8.setText(rs.getString(8));
jf8.setEditable(false);
jf9.setText(rs.getString(9));
jf9.setEditable(false);
jf10.setText(rs.getString(10));
jf10.setEditable(false);
jta.setText(rs.getString(11));
jta.setEditable(false);
jf12.setText(rs.getString(12));
jf12.setEditable(false);
jf13.setText(rs.getString(13));
jf13.setEditable(false);
if(!f)
JOptionPane.showMessageDialog(null,"此人不存在!");
rs.close();
sql.close();
con.close();
catch(SQLException p)
catch(Exception d)
public void Write()
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("insert into std values(?,?,?,?,?,?,?,?,?,?,?,?)");
sql.setString(1,jf1.getText());
sql.setString(2, jf2.getText());
sql.setString(3,jf3.getText());
sql.setString(4, jf4.getText());
sql.setString(5, jf5.getText());
sql.setString(6, jf6.getText());
sql.setString(7, jf7.getText());
sql.setString(8, jf8.getText());
sql.setString(9, jf9.getText());
sql.setString(10, jf10.getText());
sql.setString(11, jta.getText());
sql.setString(12, jf12.getText());
sql.executeUpdate();
con.close();
sql.close();
catch(Exception p)
p.printStackTrace();
public void Del()
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("delete from std where 姓名=?");
sql.setString(1, jf13.getText());
sql.executeUpdate();
sql.close();
con.close();
catch(Exception p)
p.printStackTrace();
public void Update()
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
java.sql.Connection con= DriverManager.getConnection(url,user,passwd);
PreparedStatement sql=con.prepareStatement("update std set 姓名=?,班级=?,性别=?,宿舍=?,出生年月=?,移动电话=?,民族=?,电子邮件=?,籍贯=?,备注=?,家庭详细住址=?,邮政编码=? where 姓名=?");
sql.setString(1,jf1.getText());
sql.setString(2, jf2.getText());
sql.setString(3,jf3.getText());
sql.setString(4, jf4.getText());
sql.setString(5, jf5.getText());
sql.setString(6, jf6.getText());
sql.setString(7, jf7.getText());
sql.setString(8, jf8.getText());
sql.setString(9, jf9.getText());
sql.setString(10, jf10.getText());
sql.setString(11, jta.getText());
sql.setString(12, jf12.getText());
sql.setString(13, jf13.getText());
sql.executeUpdate();
con.close();
sql.close();
catch(Exception p)
p.printStackTrace();
这是我以前写的 修改表对应的列项 就好了 参考技术B 两年前我和你有同样的想法,就是手写UI代码,告诉你,现在已经没必要这么麻烦了,下载一个net beans 吧 ,甲骨文官网里面有,里面可以像 vs.net 里面的 直接拖动,事件和属性有专门 的选项卡设置 参考技术C 我的天啊 都没进行封装啊 要的话就加我Q嘛 晕死
以上是关于详细解析java JDBC实现增删改查的主要内容,如果未能解决你的问题,请参考以下文章