如何利用java对mysql数据库进行增删改查

代码如下:
增:

@Test
	//数据插入
	public void demo1() {
		Connection conn=null;
		Statement stmt=null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//创建连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
			//执行sql对象
			 stmt=conn.createStatement();
			 String sql="INSERT INTO person VALUES(NULL,'eee','741','小白')";
			 //返回1个整型
			 int i=stmt.executeUpdate(sql);
			 if(i>0) {
				 System.out.println("插入成功!");
			 }
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			//释放资源
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn=null;
			}
			if(stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stmt=null;
			}
		}
	}

删:

@Test
	//数据删除
	public void demo3() {
		Connection conn=null;
		Statement stmt=null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
			//创建sql语句
			String sql="delete from person where id=5";
			stmt=conn.createStatement();
			int i=stmt.executeUpdate(sql);
			if(i>0) {
				System.out.println("删除成功!");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			//释放资源
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn=null;
			}
			if(stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stmt=null;
			}
		}
	}

改:

@Test
	//数据更新
	public void demo2() {
		Connection conn=null;
		Statement stmt=null;
		try {
			//注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name","root","123456");
			//创建sql语句
			String sql="update person set username='qqq',password='852',address='小陈'   where id=3";
			//创建sql执行对象
			stmt=conn.createStatement();
			int i=stmt.executeUpdate(sql);
			if(i>0) {
				System.out.println("修改成功");
			}else {
				System.out.println("修改失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			//释放资源
			if(conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				conn=null;
			}
			if(stmt!=null) {
				try {
					stmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				stmt=null;
			}
		}
	}

查:

@Test
	public void demo2() {
		try {
			//1.加载驱动
			DriverManager.registerDriver(new Driver());
			//2.创建连接
			Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/name", "root", "123456");//是不是自己的账户和密码
			//3.1创建sql语句对象
			String sql="select * from person";
			Statement stmt=conn.createStatement();
			//3.2执行sql语句并进行遍历
			ResultSet resultSet=stmt.executeQuery(sql);
			while(resultSet.next()) {
				int uid=resultSet.getInt("id");
				String username=resultSet.getString("username");
				String password=resultSet.getString("password");
				String address=resultSet.getString("address");
				System.out.println(uid+" "+username+" "+password+" "+address+" ");
			}
			//4.释放相关资源
			resultSet.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

注意:我的数据库为name,表名为person,用户名为root,密码为123456,你需要根据自己的表进行改正。
希望对你有用!

上一篇:Chrome浏览器在Windows8/8.1下显示模糊的解决办法


下一篇:JDBC连接ORACLE的三种URL格式