JDBC单元测试-------Junit删除

JunitTest:
 

package com.test.example;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.junit.Test;

import com.test.util.JDBCUtil;

/**
 * 使用Junit做单元测试
 * <p>Title: JunitTest</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
public class JunitTest {
	
//	@Test
//	public void testQuery(){
//		//查询
//		
//				Connection connection = null;
//				Statement statement = null;
//				ResultSet resultSet = null;
//				
//				try {
//					//1.获取连接对象
//				   connection = JDBCUtil.getConn();
//					//2.根据连接对象获取statement
//				   statement = connection.createStatement();
//					//3.执行sql语句,得到resultSet
//					String sql = "select * from stu";
//					resultSet = statement.executeQuery(sql);
//					//4.遍历结果集
//					while(resultSet.next()){
//						
//						String name = resultSet.getString("name");
//						
//						int age = resultSet.getInt("age");
//						
//						System.out.println("name="+name+",age="+age);
//					}
//				} catch (Exception e) {
//					// TODO: handle exception
//				}finally {
//					JDBCUtil.close(connection, resultSet, statement);
//				}
//				
//	}
//	
//	@Test
//	public void testInsert(){
//		//添加		
//		Connection connection = null;
//		Statement statement = null;
//		
//		try {
//			//1.获取连接对象
//		   connection = JDBCUtil.getConn();
//			//2.根据连接对象获取statement
//		   statement = connection.createStatement();
//			//3.执行sql语句添加
//		   String sql = "insert into stu values(32,'AAA',NULL)";
//		   //影响的行数 rs>0表明操作成功,否则操作失败
//		   int rs = statement.executeUpdate(sql);
//		   if(rs > 0){
//			   System.out.println("添加成功");
//		   }else{
//			   System.out.println("添加失败");
//		   }
//		} catch (Exception e) {
//			// TODO: handle exception
//		}finally {
//			JDBCUtil.close(connection,statement);
//		}
//	}
	
	@Test
	public void testDelete(){
		//删除		
		Connection connection = null;
		Statement statement = null;
		
		try {
			//1.获取连接对象
		   connection = JDBCUtil.getConn();
			//2.根据连接对象获取statement
		   statement = connection.createStatement();
			//3.执行sql语句添加
		   String sql = "delete from stu where name='AAA'";
		   //影响的行数 rs>0表明操作成功,否则操作失败
		   int rs = statement.executeUpdate(sql);
		   if(rs > 0){
			   System.out.println("删除成功");
		   }else{
			   System.out.println("删除失败");
		   }
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.close(connection,statement);
		}
	}

}

JDBCUtil:

package com.test.util;

import java.io.FileInputStream;
import java.io.InputStream;
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 {
	
	static String driverClass = null;
	static String url = null;
	static String name = null;
	static String password = null;
	
	//读取jdbc.properties
	static{
		try {
			//1.创建一个属性配置对象
			Properties properties = new Properties();
			
			//1.对应文件位于工程根目录  
			//InputStream is = new FileInputStream("jdbc.properties");
			
			//2.使用类加载器,读取drc下的资源文件  对应文件位于src目录底下  建议使用
			InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			//2.导入输入流,抓取异常
			properties.load(is);
			//3.读取属性
			driverClass = properties.getProperty("driverClass");
			url = properties.getProperty("url");
			name = properties.getProperty("name");
			password = properties.getProperty("password");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 注册驱动 建立参数
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	
	public static Connection getConn(){
			
		Connection connection = null;
		//2. 建立连接 参数一: 协议 + 访问的数据库 , 参数二: 用户名 , 参数三: 密码。
		 try {
			 //Class.forName(driverClass);可写可不写
			 //Class.forName(driverClass);
			connection = DriverManager.getConnection(url, name, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return connection;
		
	}
	
	/**
	 * 释放资源
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	public static void close(Connection connection,ResultSet resultSet,Statement statement){
		closeRS(resultSet);
		closeSt(statement);
		closeConn(connection);
		
	}
	public static void close(Connection connection,Statement statement){
		closeSt(statement);
		closeConn(connection);
		
	}
	
	private static void closeRS(ResultSet resultSet){
		try {
			if(resultSet !=null){
				resultSet.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			resultSet = null;
		}
	}
	
	private static void closeSt(Statement statement){
		try {
			if(statement !=null){
				statement.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			statement = null;
		}
	}
	
	private static void closeConn(Connection connection){
		try {
			if(connection !=null){
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			connection = null;
		}
	}
	


}


jdbc.properties:

driverClass=com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/jdbc
name = root
password = root


数据库:

运行结果:

猜你喜欢

转载自blog.csdn.net/mqingo/article/details/84527904
今日推荐