改进版-标准-JDBC-项目示例二

来源:互联网 发布:十大网络主播评选 编辑:程序博客网 时间:2024/06/14 08:46

1.软件包规范的结构:

2.开始步骤顺序:

3.代码示范:

<span style="font-size:18px;">package com.yunhe.bean;public class BanJi {private int id;private String name;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 BanJi() {super();// TODO Auto-generated constructor stub}public BanJi(int id, String name) {super();this.id = id;this.name = name;}@Overridepublic String toString() {return "BanJi [id=" + id + ", name=" + name + "]";}}</span>

-----------------------------
<span style="font-size:18px;"><span style="font-size:18px;">package com.yunhe.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class JDBCUtil {private static String url;private static String user;private static String password;static {try {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static Connection getConnection() throws SQLException {Properties properties = new Properties();try {properties.load(new FileInputStream(new File("cfg/jdbc.properties")));url = properties.getProperty("url");user = properties.getProperty("user");password = properties.getProperty("password");} catch (IOException e) {e.printStackTrace();}return DriverManager.getConnection(url, user, password);}public static void free(ResultSet rs, Statement stmt, Connection conn) {try {if (rs != null)rs.close();} catch (SQLException e) {e.printStackTrace();} finally {rs = null;try {if (stmt != null)stmt.close();} catch (SQLException e) {e.printStackTrace();} finally {stmt = null;try {if (conn != null)conn.close();} catch (SQLException e) {e.printStackTrace();} finally {conn = null;}}}}}</span></span>

---------------------------------------------------

改进的程序:

建立一个接口类

package com.yunhe.dao;import java.sql.SQLException;import java.util.List;import com.yunhe.bean.BanJi;public interface BanJiDao {public abstract int addBanJi(BanJi banJi) throws SQLException;public int editBanJiById(BanJi banJi) throws SQLException;public int delBanJiById(int id) throws SQLException;public BanJi findBanJiById(int id) throws SQLException;public List<BanJi> findAllBanJis() throws SQLException;}

-------------------------------------------

再建一个接口实现类:

package com.yunhe.dao.impl;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 com.yunhe.bean.BanJi;import com.yunhe.dao.BanJiDao;import com.yunhe.util.JDBCUtil;public class BanJiDaoImpl implements BanJiDao {@Overridepublic int addBanJi(BanJi banJi) throws SQLException {Connection conn = JDBCUtil.getConnection();// ? 问号 占位符PreparedStatement ps = conn.prepareStatement("insert into tb_banJi(id,name) values(?,?)");ps.setInt(1, banJi.getId());ps.setString(2, banJi.getName());int res = ps.executeUpdate();if (1 == res)System.out.println("add success");return res;}@Overridepublic int editBanJiById(BanJi banJi) throws SQLException {String sql = "update tb_banJi set name=? where id=?";Connection conn = JDBCUtil.getConnection();PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(2, banJi.getId());// 注意顺序不能写反,看问号?的位置ps.setString(1, banJi.getName());int res = ps.executeUpdate();if (1 == res)System.out.println("editBanJi success");return res;}@Overridepublic int delBanJiById(int id) throws SQLException {Connection conn = JDBCUtil.getConnection();String sql = "delete from tb_banJi where id=?";PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, id);int res = ps.executeUpdate();if (1 == res)System.out.println("delBanJi success");return res;}@Overridepublic BanJi findBanJiById(int id) throws SQLException {Connection conn = JDBCUtil.getConnection();String sql = "select id,name from tb_banJi where id=?";PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, id);ResultSet res = ps.executeQuery();BanJi banJi = null;while (res.next()) {banJi = new BanJi();banJi.setId(res.getInt("id"));banJi.setName(res.getString("name"));}return banJi;}@Overridepublic List<BanJi> findAllBanJis() throws SQLException {Connection conn = JDBCUtil.getConnection();String sql = "select id,name from tb_banJi";PreparedStatement ps = conn.prepareStatement(sql);ResultSet res = ps.executeQuery();List<BanJi> banjilist = new ArrayList<BanJi>();while (res.next()) {BanJi banJi;banJi = new BanJi();banJi.setId(res.getInt("id"));banJi.setName(res.getString("name"));banjilist.add(banJi);}return banjilist;}}

建立一个接口实现类?的测试类 Test

package com.yunhe.dao;import java.sql.SQLException;import java.sql.Timestamp;import java.util.Date;import java.util.List;import org.junit.Test;import com.yunhe.bean.BanJi;import com.yunhe.dao.impl.BanJiDaoImpl;public class BanJiDaoTest {@Testpublic void timefun() {// 获取计算机元年时间: java.sql.TimestampTimestamp timestamp = new Timestamp(0);System.out.println(":" + timestamp);// 获取当时时间的一种方式timestamp = new Timestamp(new Date().getTime());System.out.println("当前时间是:" + timestamp);// 获取当时时间的第二种方式timestamp = new Timestamp(System.currentTimeMillis());System.out.println(timestamp);Date date = new java.sql.Date(System.currentTimeMillis());System.out.println("java.sql.Date()不显示时分秒 :" + date);// java.sql.Timestamp 可以转化为 java.utli.Datedate = new Timestamp(20);System.out.println(date);}@Testpublic void testAddBanJi() {BanJiDaoImpl dao = new BanJiDaoImpl();try {dao.addBanJi(new BanJi(188, "kexue"));} catch (SQLException e) {e.printStackTrace();}}@Testpublic void testEditBanJiById() {BanJiDaoImpl dao = new BanJiDaoImpl();try {BanJi banJi = new BanJi(1002, "a");dao.editBanJiById(banJi);} catch (SQLException e) {e.printStackTrace();}}@Testpublic void testDelBanJiById() {BanJiDaoImpl dao = new BanJiDaoImpl();try {dao.delBanJiById(108);} catch (SQLException e) {e.printStackTrace();}}@Testpublic void testFindBanJiById() {BanJiDaoImpl dao = new BanJiDaoImpl();try {BanJi res = dao.findBanJiById(1002);System.out.println(res);} catch (SQLException e) {e.printStackTrace();}}@Testpublic void testFindAllBanJis() {BanJiDaoImpl dao = new BanJiDaoImpl();try {List<BanJi> res = dao.findAllBanJis();for (BanJi temp : res)System.out.println(temp);} catch (SQLException e) {e.printStackTrace();}}}




----------------------

存储账号 密码

<span style="font-size:24px;">url=jdbc:mysql://localhost:3306/testuser=rootpassword=root</span>


注意:


0 0
原创粉丝点击