JDBC中

1.JDBC封装

public static void main(String[] args) {
	 String name = "admin";
	 String password="root";
	 String sql = "select * from student where user_name='"+name+"'and'"+password+"'";

	 class RowMapper implements IRowMapper{
		 
		 
		 public void rowMapper (ResultSet resultSet) {
			 try {
				if(resultSet.next()) {
					 System.out.println("yes");
				 }else {
					 System.out.println("no");
				 }
			} catch (SQLException e) {
				e.printStackTrace();
			}
		 }
	 }
	 IRowMapper rowMapper = new RowMapper();//上转型对象
	 select(sql,rowMapper);

}
public static void select(String sql,IRowMapper rowMapper){

	try {
		Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null ; 
	try {
		connection = DriverManager.getConnection("jdbc:mysql://101.200.43.31:3306/venus", "root", "root");
		statement = connection.createStatement();	
		resultSet = statement.executeQuery(sql);
		rowMapper.rowMapper(resultSet);//多态,实际执行实现类
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		try {
			if (resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (statement!=null) {
				statement.close();
			}//不加if如果为空会报空指针异常
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
}

2.数据库工具类

package com.zzu.tool.db;

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

/**
 * 数据库类
 *
 * @author 康靖雯
 */
public class DBTool {
	
	static {
		//加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取数据库连接
	 *
	 * @author 康靖雯
	 */
	private static Connection getConnection() {
		try {
			return DriverManager.getConnection("jdbc:mysql://101.200.43.31:3306/venus", "root", "root");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 *关闭数据库资源 (Connection和Statement)
	 *
	 * @author 康靖雯
	 */
	private static void close(Connection connection,Statement statement) {
		try {
			if (statement!=null) {
				statement.close();
			}//不加if如果为空会报空指针异常
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (connection!=null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 关闭数据库资源(Connection,Statement和ResultSet)
	 *
	 * @author 康靖雯
	 */
	private static void close(Connection connection,Statement statement,ResultSet resultSet) {
		try {
			if (resultSet!=null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		close(connection,statement);	
	}
	
	/**
	 * 数据修改(insert,delete,update)
	 *
	 * @author 康靖雯
	 */
	public static boolean update(String sql) {
		
		Connection connection = getConnection();
		Statement statement = null;
		//建立java与MySQL数据库连接
		try {
			statement = connection.createStatement();//相当于打开了修改的窗口
			int rows = statement.executeUpdate(sql);//此时执行相当于点了run
			return rows>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close(connection,statement);
		}
		return true;		
	}
	
	/**
	 * 数据查询
	 *
	 * @author 康靖雯
	 */
	public static void select(String sql,IRowMapper rowMapper){

		Connection connection = getConnection();
		Statement statement = null;
		ResultSet resultSet = null ; 
		try {
			statement = connection.createStatement();	
			resultSet = statement.executeQuery(sql);
			rowMapper.rowMapper(resultSet);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (resultSet!=null) {
					resultSet.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			close(connection,statement,resultSet);
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值