JAVA利用JDBC对数据库进行增删改查

27 篇文章 1 订阅

package Test1;

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

public class Test {
public static Connection connection=null;
public static Statement statement=null;

public static void main(String[] args) {
	//Insert();
	//Delete(1);
	Update(3);
//	Select();
}

public static void Select(){
	
	try {
		//注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//获取连接对象
		 connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
		
		//写SQL语句
		String sql = "select * from student";
		
		//获取执行对象
		statement = connection.createStatement();
		
		//执行SQL语句
		
		ResultSet rows= statement.executeQuery(sql);
		System.out.println("id"+"\t"+"姓名");
		//处理结果
	while(rows.next()){
		int id = rows.getInt("id");
		String name = rows.getString("name");
		System.out.println(id+"\t"+name);
	}

	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		System.out.println("驱动注册异常");
	}catch(SQLException e){
		System.out.println("数据库连接异常");
	}finally {
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if(statement != null){
			try{
				statement.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
}
public static void Update(int id){
	
	try {
		//注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//获取连接对象
		 connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
		
		//写SQL语句
		String sql = "update  student set name='hehhehe' where id="+id;
		
		//获取执行对象
		statement = connection.createStatement();
		
		//执行SQL语句
		
		int row= statement.executeUpdate(sql);
		
		//处理结果
		if(row > 0){
			System.out.println("更改成功");
		}else {
			System.out.println("更改失败");
		}

	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		System.out.println("驱动注册异常");
	}catch(SQLException e){
		System.out.println("数据库连接异常");
	}finally {
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if(statement != null){
			try{
				statement.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
}
public static void Delete(int id){
	
	try {
		//注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//获取连接对象
		 connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
		
		//写SQL语句
		String sql = "delete from student where id="+ id;
		
		//获取执行对象
		statement = connection.createStatement();
		
		//执行SQL语句
		
		int row= statement.executeUpdate(sql);
		
		//处理结果
		if(row > 0){
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}

	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		System.out.println("驱动注册异常");
	}catch(SQLException e){
		System.out.println("数据库连接异常");
	}finally {
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if(statement != null){
			try{
				statement.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
}

public static void Insert(){
	
	try {
		//注册驱动
		Class.forName("com.mysql.jdbc.Driver");
		//获取连接对象
		 connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/xx", "root", "root");
		
		//写SQL语句
		String sql = "insert into student values (6,'hy',45,'女','100')";
		
		//获取执行对象
		statement = connection.createStatement();
		
		//执行SQL语句
		
		int row= statement.executeUpdate(sql);
		
		//处理结果
		if(row > 0){
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}

	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		System.out.println("驱动注册异常");
	}catch(SQLException e){
		System.out.println("数据库连接异常");
	}finally {
		if(connection!=null){
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if(statement != null){
			try{
				statement.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
	
}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值