1.创建项目,导入mysql-connector-java-*.jar包

2.实现增删改查

1)连接数据库

public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// 数据库
		//class.forname()
        Class.forName("com.mysql.jdbc.Driver");
        //Driver
        Connection con=null;
        String dburl="jdbc:mysql://localhost:3306/jdbc_app";
        String user="root";
        String password="";
        con=DriverManager.getConnection(dburl, user, password);
        System.out.println("con="+con);
	}

2)创建表

@Test
	public void testForCreateTable() {
		Connection con=null;
		Statement st=null;
		String sql="create table tb_userLogin(userName varchar(15) primary key,loginPwd varchar(15))";
		//获取数据库连接
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_app","root","");
			if(con!=null) {
				st=con.createStatement();
				//执行sql语句
				int n=st.executeUpdate(sql);
				System.out.println("n="+n);
				System.out.println(n>=0?"创建表成功":"操作失败");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(st!=null) {
				try {
					st.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con!=null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
		
	}

3)插入数据

@Test
	public void testForInsert() {
		String sql="insert into tb_user(id,userName,userPwd,mobile) values(?,?,?,?)";
		Connection con=null;
		User user=new User("10002","b","111","123456");
		PreparedStatement ps=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_app","root","");
			if(con!=null) {
				ps=con.prepareStatement(sql);//执行预编译
				//给对应的动态sql中的问号位置赋值
				ps.setString(1, user.getId());
				ps.setString(2, user.getUserName());
				ps.setString(3, user.getUserPwd());
				ps.setString(4, user.getMobile());
				int n=ps.executeUpdate();//执行更新
				System.out.println(n>0?"插入成功":"失败");
				
			}
			ps.close();
			con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(ps!=null) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(con!=null) {
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

4)查询数据

@Test
	public void testForSelect() {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		User user=null;
		String sql="select * from tb_user";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_app","root","");
			if(con!=null) {
				ps=con.prepareStatement(sql);
				rs=ps.executeQuery();
				while(rs.next()) {
					user=new User();
					//获取列的值
					String uid=rs.getString(1);
					user.setId(uid);
					user.setUserName(rs.getString(2));
					user.setUserPwd(rs.getString("userPwd"));
					user.setMobile(rs.getString("mobile"));
					System.out.println(user);
				}
				rs.close();
				ps.close();
				con.close();
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

 5)更新数据

@Test
	public void testForUpdate() {
		Connection con=null;
		PreparedStatement ps=null;
		String sql="update tb_user set mobile=?,userPwd=?,userName=? where id=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_app","root","");
			if(con!=null) {
				ps=con.prepareStatement(sql);
				ps.setString(4, "10002");
				ps.setString(1, "666");
				ps.setString(2, "888");
				ps.setString(3, "b");
				int n=ps.executeUpdate();
				System.out.println(n>0?"更新成功":"失败");
			}
			ps.close();
			con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

6)删除数据

@Test
	public void testForDelete() {
		Connection con=null;
		PreparedStatement ps=null;
		String sql="delete from tb_user where id=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc_app","root","");
			if(con!=null) {
				ps=con.prepareStatement(sql);
				ps.setString(1, "10001");
				int n=ps.executeUpdate();
				System.out.println(n>0?"删除成功":"失败");
			}
			ps.close();
			con.close();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

3.查询数据库,运行成功。

 

Logo

本社区面向用户介绍CSDN开发云部门内部产品使用和产品迭代功能,产品功能迭代和产品建议更透明和便捷

更多推荐