JDBC的基本用法

数据库建立连接

/*
 *  测试跟数据库建立连接
 */
public class Demo01 {
	public static void main(String[] args) {
		Connection conn = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			long start = System.currentTimeMillis();
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			long end = System.currentTimeMillis();
			System.out.println(conn);
			System.out.println("建立连接,耗时:" + (end - start) + "ms毫秒");

			// 有了 Connection 对象之后可以进行查询了
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
}

Statement接口的用法,执行SQL语句,以及SQL注入问题

/*
 *  测试Statement接口的用法,执行SQL语句,以及SQL注入问题
 */
public class Demo02 {
	public static void main(String[] args) {
		Connection conn = null;
		Statement stmt = null ;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");

			// 有了 Connection 对象之后可以进行查询了
			// 获得连接
			stmt = conn.createStatement();
			// String name = "茂茂";
			// String sql = "insert into t_user (username,pwd,regTime) values
			// ('"+name+"',55555,now())";
			// stmt.execute(sql);

			// 测试SQL注入
			// 这样就是SQL注入,比较危险,因为只用随意加一些or 1=1(语句)就能够随意修改数据库
			String id = "5 or 1=1 ";
			String sql = "delete from t_user where id=" + id;
			stmt.execute(sql);

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 后开的先关
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

PreparedStatement的基本用法

/*
 *  测试PreparedStatement的基本用法
 *  当传入的参数为一种参数类型时
 */
public class Demo03 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			// ? 为占位符:防止SQL注入问题
			String sql = "insert into t_user (username,pwd) values (?,?)"; // ? 为占位符(先占住一个位置,等等在传入一个参数)
			ps = conn.prepareStatement(sql);
			// 1代表第一个 ?
			// 传入的参数为一种类型
			ps.setString(1, "林伟茂6"); // 参数索引是从1开始计算,而不是 0
			ps.setString(2, "123456");

			System.out.println("插入一行记录");
			// 执行上述语句
			ps.execute();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 后开的先关
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
/*
 *  测试PreparedStatement的基本用法
 *  当传入的参数为多种类型时
 */
public class Demo03_1 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			// ? 为占位符:防止SQL注入问题
			String sql = "insert into t_user (username,pwd,regTime) values (?,?,?)"; // ? 为占位符(先占住一个位置,等等在传入一个参数)
			ps = conn.prepareStatement(sql);
			// 1代表第一个 ?
			// 传入的参数为一种类型
//			 ps.setString(1, "林伟茂"); // 参数索引是从1开始计算,而不是 0
//			 ps.setString(2, "123456");

			
			// 传入时间
			// ps.setString(1, "林伟茂2"); // 参数索引是从1开始计算,而不是 0
			// ps.setString(2, "456");
			// ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));

			// 可以使用setObject方法处理参数
			// 当传入的参数为多种类型时
	
			ps.setObject(1, "林伟茂2");
			ps.setObject(2, 123);
			ps.setObject(3, new java.sql.Date(System.currentTimeMillis()));
			System.out.println("插入一行记录");
			// 执行上述语句
			ps.execute();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			// 后开的先关
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
/*
 *  测试PreparedStatement的基本用法
 *  executeUpdate的用法
 */
public class Demo03_2 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			// ? 为占位符:防止SQL注入问题
			String sql = "insert into t_user (username,pwd,regTime) values (?,?,?)"; // ? 为占位符(先占住一个位置,等等在传入一个参数)
			ps = conn.prepareStatement(sql);
			// 1代表第一个 ?
			// 传入的参数为一种类型
			// ps.setString(1, "林伟茂"); // 参数索引是从1开始计算,而不是 0
			// ps.setString(2, "123456");

			// 传入时间
			// ps.setString(1, "林伟茂2"); // 参数索引是从1开始计算,而不是 0
			// ps.setString(2, "456");
			// ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));

			// 可以使用setObject方法处理参数
			// 当传入的参数为多种类型时

			ps.setObject(1, "林伟茂2");
			ps.setObject(2, 123);
			ps.setObject(3, new java.sql.Date(System.currentTimeMillis()));
			System.out.println("插入一行记录");
			// 执行上述语句
			// ps.execute();
			// 返回的为一个数(这个数为更新影响的行数)
			int count = ps.executeUpdate();
			System.out.println(count);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 后开的先关
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

ResultSet结果集的基本用法

/*
 *  测试ResultSet结果集的基本用法
 */
public class Demo04 {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
			// 真正开发中,为了提高效率,都会使用连接池来管理连接对象!
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/testjdbc?useUnicode=true&characterEncoding=UTF-8", "root", "root");
			// ? 为占位符:防止SQL注入问题
			String sql = "select id,username,pwd from t_user where id > ?"; // ? 为占位符(先占住一个位置,等等在传入一个参数)
			ps = conn.prepareStatement(sql);
			// 把 id大于2的记录都取出来
			ps.setObject(1, 2);
			// ResultSet 结果集
			rs = ps.executeQuery();
			// rs.next()为游标,如果有数据就返回 true
			while(rs.next()) {
				// rs.getInt(1):取出记录的第一列
				System.out.println(rs.getInt(1)+"-----"+rs.getString(2)+"-----"+rs.getString(3));
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			// 后开的先关
			// 关闭顺序遵循:ResultSet --> Statement --> Connection这样的关闭顺序!一定要将三个trycatch块分开写!
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			// 关闭 Connection
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
发布了45 篇原创文章 · 获赞 1 · 访问量 5218

猜你喜欢

转载自blog.csdn.net/weixin_42814000/article/details/104577426