1.测试类的内容:
在包:com.hanchao.test中
- package com.hanchao.test;
-
- import com.hanchao.dao.UserDao;
- import com.hanchao.entity.User;
-
-
- public class Test {
-
-
-
- public static void main(String[] args) {
-
-
-
-
-
-
-
-
- // UserDao userDao = new UserDao();
- // userDao.delete(21);
-
-
- UserDao userDao = new UserDao();
- userDao.retrieve(22);
-
- }
-
- }
2.实体类的写法:com.hanchao.entity
- package com.hanchao.entity;
-
- public class User {
-
-
-
- private int id;
- private String username;
- private String address;
-
- //下面是setter...getter..
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
-
-
- }
3.dao的写法:com.hanchao.dao
- package com.hanchao.dao;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import com.hanchao.entity.User;
-
-
- public class UserDao {
-
-
-
-
- public int insert(User user) {
- Connection con = null;
- PreparedStatement sta = null;
- int rows = 0;
-
- try {
-
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
-
- String sql = "insert into t_user(username,address) value(?,?)";
- sta = con.prepareStatement(sql);
- sta.setString(1, user.getUsername());
- sta.setString(2, user.getAddress());
-
- rows = sta.executeUpdate();
- if(rows > 0) {
- System.out.println("operate successfully!!");
- }
-
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(sta != null) {
- try {
- sta.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- return rows;
- }
-
-
- public int update(User user) {
- Connection con = null;
- PreparedStatement sta = null;
- int rows = 0;
-
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
-
- String sql = "update t_user set address=?,username=? where id=?";
- sta = con.prepareStatement(sql);
- sta.setString(1, user.getAddress());
- sta.setString(2, user.getUsername());
- sta.setInt(3, user.getId());
-
- rows = sta.executeUpdate();
- if(rows > 0) {
- System.out.println("ok");
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(sta != null) {
- try {
- sta.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- return rows;
- }
-
-
- public int delete(int id) {
- Connection con = null;
- PreparedStatement sta = null;
- int rows = 0;
-
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
-
- String sql = "delete from t_user where id=?";
- sta = con.prepareStatement(sql);
- sta.setInt(1, id);
-
- rows = sta.executeUpdate();
- if(rows > 0) {
- System.out.println("ok,ok,ok");
- }
-
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(sta != null) {
- try {
- sta.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- return rows;
- }
-
-
-
- public void retrieve(int id) {
- Connection con = null;
- PreparedStatement sta = null;
- ResultSet rs = null;
-
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
-
- String sql = "select id,username,address from t_user where id=?";
- sta = con.prepareStatement(sql);
- sta.setInt(1, id);
-
- rs = sta.executeQuery();
- if(rs.next()) {
- int idd = rs.getInt("id");
- String username = rs.getString("username");
- String adrress = rs.getString("address");
- System.out.println(idd+"\t"+username+"\t"+adrress);
- }
-
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(sta != null) {
- try {
- sta.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if(con != null) {
- try {
- con.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- }
- }
-
- }
看dao中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化!
我把前三篇文章的例子,包括数据库,放在一个压缩包里!名称为:jdbc学习例子.rar。
需要的同学可以去下载!!