数据库的连接操作

  • 今日计划

  1. 数据库连接以及关闭操作等代码部分练习
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBUtils {
	/**
	 * 数据库连接操作
	 * */
	static String url="jdbc:mysql://localhost:3306/haha";
	static String user="root";
	static String password="wy123";
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 创建数据库连接
	 * @throws SQLException 
	 * */
	public Connection getConnection() throws SQLException{
		Connection conn=DriverManager.getConnection(url, user, password);
		return null;
	}
	/**
	 * 关闭数据库操作1:PreparedStatement ps,Connection conn
	 * */
	public static void close1(PreparedStatement ps,Connection conn){
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	/**
	 * 数据库连接操作2:
	 * */
	public static void close2(ResultSet rs,PreparedStatement ps,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		close1(ps, conn);
	}
}
  1. 数据库的插入数据信息的代码部分练习
@Override
	public boolean insert(User user) {
		boolean flag=false;
		Connection conn=null;
		PreparedStatement ps=null;
		try {
		//获取数据库连接
			conn=DBUtils.getConnection();
			//创建并执行SQL语句
			ps=conn.prepareStatement("insert into user(name,password,logintime)value(?,?,?)");
			//通过prepareStatement对象设置属性具体数值
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));
			//执行sql语句,int n用来判断插入的语句个数是否为0
			int n=ps.executeUpdate();
			if(n>0){
				flag=true;
			}
			//关闭数据库连接
			DBUtils.close1(ps, conn);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}

在JDBC中prepareStatement接口提供了execute,executeQuery和executeUpdate等三个执行SQL的语句方法,三个方法的区别:

  •  executeQuery:用于产生单个结果集的语句,例如SELECT,这个方法最多用来使用SELECT查询
  •  executeUpdate:用于执行用于执行 INSERT,UPDATE,DELETE等语句,其返回值是一个整数,表示受到该语句影响的行数
  •  execute:用于执行返回多个结果集,多个更新计数
  1. 数据库删除数据部分代码练习
//代码与插入类似
@Override
	public boolean delete(User user) {
		// TODO Auto-generated method stub
		boolean flag=false;
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn=DBUtils.getConnection();
			ps=conn.prepareStatement("delete from user where name=?");
			ps.setString(1, user.getName());
			int n=ps.executeUpdate();
			if(n>0){
				flag=true;
			}
			DBUtils.close1(ps, conn);
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return flag;
	}
  1. 数据库更新数据部分代码练习
@Override
	public boolean update(User user) {
		// TODO Auto-generated method stub
		boolean flag=false;
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			conn=DBUtils.getConnection();
			ps=conn.prepareStatement("update user set password=? where name=?");
			ps.setString(1, user.getName());
			ps.setString(2, user.getPassword());
			int n=ps.executeUpdate();
			if(n>0){
				flag=true;
			}
			DBUtils.close1(ps, conn);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return flag;
	}
  •  总结:对于数据库的插入,删除,更新等三个操作因为不需要返回结果集,所以用到的语句雷同,只是SQL语句不同
原文地址:https://www.cnblogs.com/sinon/p/5825478.html