JDBC入门(一)基本CRUD操作

java连接数据库步骤(mysql):

加载驱动,获取连接,创建处理命令,执行sql命令,处理结果,关闭资源.


public class JDBCCrud {

	/**
	 * 向表中添加数据 
	 */
	public void insert(String username,String password){
		Connection conn = null;
		Statement stmt = null;
		try {
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获取连接
			conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=9527");
			//创建处理命令
			stmt = conn.createStatement();
			//执行sql命令
			int i = stmt.executeUpdate(
					"insert into user(username,password) values('"+username+"','"+password+"')");
			//处理结果
			if(i>0){
				System.out.println("操作成功,影响数据行数:"+i);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				//alt+shift+z
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void login(String username,String password){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=9527");
			stmt = conn.createStatement();
			//执行查询操作获取结果集
			rs = stmt.executeQuery("select * from user where username='"+username+"' and password='"+password+"'");
			//取出结果集中的数据
			if(rs.next()){
				//根据数据表中的列名称以及类型从结果集中取出指定的列数据
				int id = rs.getInt("id");
				String uname = rs.getString("username");
				String pword = rs.getString("password");
				System.out.println(id+"--"+uname+"--"+pword);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs != null)rs.close();
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 *查询所有操作
	 *
	 */
	public void queryAll(){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=9527");
			stmt = conn.createStatement();
			//执行查询操作获取结果集
			rs = stmt.executeQuery("select id,username,password from user");
			//取出结果集中的数据
			while(rs.next()){
				//根据数据表中的列名称以及类型从结果集中取出指定的列数据
				int id = rs.getInt("id");
				String uname = rs.getString("username");
				String pword = rs.getString("password");
				System.out.println(id+"--"+uname+"--"+pword);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs != null)rs.close();
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 删除数据操作
	 */ 
	public void delete(int id){
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=9527");
			stmt = conn.createStatement();
			int i = stmt.executeUpdate("delete from user where id="+id+"");
			if(i > 0){
				System.out.println("操作成功,影响数据行数:"+i);
				System.out.println("删除"+id+"号用户成功!");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 更新数据操作
	 */
	public void updete(int id,String username,String password){
		Connection conn = null;
		Statement stmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb?user=root&password=9527");
			stmt = conn.createStatement();
			int i = stmt.executeUpdate(
					"update user set username='"+username+"',password='"+password+"' where id="+id+"");
			if(i>0){
				System.out.println("操作成功,影响数据行数:"+i);
				System.out.println("修改成功!");
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}	
	}
	
	
	public static void main(String[] args) {
		int id = 1;
		String username = "rose";
		String password = "654321";
		JDBCCrud jdbc = new JDBCCrud();
		//jdbc.insert(username,password);
		//jdbc.queryAll();
		//jdbc.updete(id, username, password);
		//jdbc.delete(id);
		jdbc.login(username,password);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值