码迷,mamicode.com
首页 > 数据库 > 详细

JDBC 基本操作

时间:2020-04-20 09:13:00      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:resultset   inno   new   mamicode   now()   tar   test   statement   mysq   

先搞个表

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(10) DEFAULT NULL,
  `pwd` varchar(10) DEFAULT NULL,
  `regTime` date DEFAULT NULL,
  `lastLoginTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20213 DEFAULT CHARSET=utf8;

技术图片

基本操作

// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			long start = System.currentTimeMillis();
			// 创建连接
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			long end = System.currentTimeMillis();
			System.out.println("建立连接耗时:-->" + (end-start) +" ms");
			System.out.println(conn);

Sql注入 Statement

public class Demo02 {

	public static void main(String[] args) {
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			System.out.println(conn);
			
			Statement statement = conn.createStatement();
			//statement.execute("insert into t_user(username, pwd, regTime) values(‘张三‘,‘123‘,now())");
			
			// 测试 SQL 注入
			String id = "5 or 1=1";
			statement.execute("delete from t_user where id = " + id);
			
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			// 关闭资源
		}
	}
}

PreparedStatement基本用法

// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			
			String sql = "insert into t_user (username, pwd, regTime) values(?, ?, ?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			/*ps.setString(1, "李四");
			ps.setString(2, "123456");
			ps.setDate(3, new Date(System.currentTimeMillis()));*/
			ps.setObject(1, "王五");
			ps.setObject(2, "987654");
			ps.setObject(3, new Date(System.currentTimeMillis()));
			ps.execute();

ResutlSet基本用法

public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			
			String sql = "select * from t_user where id > ?";
			ps = conn.prepareStatement(sql);
			
			ps.setObject(1, 3);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				System.out.println(rs.getInt(1) + "--" + rs.getString(2) + "--" + rs.getString(3));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps != null) {
					ps.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

JDBC 基本操作

标签:resultset   inno   new   mamicode   now()   tar   test   statement   mysq   

原文地址:https://www.cnblogs.com/xzlf/p/12735544.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!