TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
1.测试类的内容:
在包:com.hanchao.test中
- [/code]
-
- package com.hanchao.test;
-
- import com.hanchao.dao.UserDao;
- import com.hanchao.entity.User;
-
- /**
- * 测试jdbc的类(javaBean+DAO)
- * @author hanlw
- * 2012-07-09
- */
- public class Test {
-
- /**
- * 1.什么是JavaBean?
- * 它要满足两个条件:<1>.私有化属性;<2>.公开访问方法(setter and getter)
- *
- * 2.javabean又名:entity实体类、pojo、vo
- *
- * 3.DAO = data access object (封装了对一个表的所有CRUD操作!!)
- * DAO中方法的本质是将对象(entity)类和数据库进行交互。
- */
-
- public static void main(String[] args) {
-
- /**
- * 1.insert()操作测试
- */
- /* UserDao userDao = new UserDao();
- User user = new User();
-
- user.setUsername("chenchen");
- user.setAddress("jiangsu");
- userDao.insert(user);
- */
-
- /**
- * 2.update()操作
- */
- /* UserDao userDao = new UserDao();
- User user = new User();
-
- user.setId(20);
- user.setAddress("yancheng1");
- user.setUsername("iloveyou");
- userDao.update(user);
- */
-
- /**
- * delete()操作
- */
- // UserDao userDao = new UserDao();
- // userDao.delete(21);
-
- /**
- * retrieve()操作
- */
- UserDao userDao = new UserDao();
- userDao.retrieve(22);
-
- }
-
- }
-
- 2.实体类的写法:com.hanchao.entity
-
- [code]
复制代码
package com.hanchao.entity;
/**
* 实体类
* @author hanlw
* 2012-07-09
*/
public class User {
/**
* 1.实体类的类名一般和数据库中的相应的表名相同:如t_user对应的实体类为User
*
* 2.实体类中的属性一般与表中的列名相同:如下
*
* 3.写属性的getter 和 setter方法
*/
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
[code][/code]
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;
/**
* User类对应的Dao
* @author hanlw
* 2012-07-09
*/
public class UserDao {
/**
* 说明:
* 1.某个实体类对应的Dao一般写成(类名+Dao),如User类对应Dao类为:UserDao
*
* 2.Dao中封装了对对应实体类的所有的CRUD的操作!!如下:
*/
/**
* 1.对mysql的insert操作
*/
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;
}
/**
* 2.对mysql的update操作
*/
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;
}
/**
* 3.对mysql的delete()操作
*/
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;
}
/**
* 4.retrieve()操作
*/
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+" "+username+" "+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中的内容,我们发现几个方法中的代码有很多重复的!!所以在下一篇文章中,我们要对代码进行优化!
出处http://hanchaohan.blog.51cto.com/2996417/925573 |
|