jdbc访问数据库实现原理
文章目录
1.注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
2.获取连接
`Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jt_db?characterEncoding=utf-8",
"root", "root");`
3.获取传输器
Statement stat = conn.createStatement();
4.利用传输器发送SQL到服务器执行,返回执行结果
`String sql = "select * from account";
ResultSet rs = stat.executeQuery(sql);`
5.处理结果
6.防止注入攻击方法
`ComboPooledDataSource pool = new ComboPooledDataSource();
Connection conn = pool.getConnection();
String sql = "select * from user";
PreparedStatement ps =conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();`
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论