JDBC 入门
1.创建JDBC与数据库的链接
0)导入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCDemo1 {
@Test
// JDBC入门程序
public void demo1(){
/**
* 1.注册驱动
* 2.获得连接
* 3.执行sql
* 4.释放资源
*/
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
// 1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
// DriverManager.registerDriver(new Driver());
// 2.获得连接
conn = DriverManager.getConnection("jdbc:mysql:///day03", "root", "123");
// 3.执行SQL语句
// 3.1创建执行sql语句的对象:
stmt = conn.createStatement();
// 3.2执行sql语句
rs = stmt.executeQuery("select * from user");
// 3.3遍历结果集
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password")+" "+rs.getString("nickname")+" "+rs.getDouble("salary"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 4.释放资源
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
02 使用Statement连接 并作增删改查操作
/**
* 完成JDBC的CRUD的操作
* @author jt
*
*/
public class JDBCDemo2 {
@Test
// 查询一条记录
public void demo4(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 获得连接
conn = DriverManager.getConnection("jdbc:mysql:///day03", "root", "123");
// 创建执行SQL对象
stmt = conn.createStatement();
String sql = "select * from user where id = 2";
rs = stmt.executeQuery(sql);
// 不用遍历结果集
if(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password")+" "+rs.getString("nickname")+" "+rs.getDouble("salary"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 4.释放资源
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
@Test
// 删除操作
public void demo3(){
Connection conn = null;
Statement stmt = null;
try{
// 1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获得连接
conn = DriverManager.getConnection("jdbc:mysql:///day03", "root", "123");
// 3.创建执行SQL对象:
stmt = conn.createStatement();
String sql = "delete from user where id = 3";
int num = stmt.executeUpdate(sql);
if(num > 0){
System.out.println("删除成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 4.释放资源
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
@Test
// 修改操作
public void demo2(){
Connection conn = null;
Statement stmt = null;
try{
// 1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获得连接
conn = DriverManager.getConnection("jdbc:mysql:///day03", "root", "123");
// 3.执行SQL
stmt = conn.createStatement();
String sql = "update user set username='qwe',password='zxc',nickname='王yy',salary=4800 where id = 6";
int num = stmt.executeUpdate(sql);
if(num > 0){
System.out.println("修改成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 4.释放资源
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
@Test
// 保存操作
public void demo1(){
Connection conn = null;
Statement stmt = null;
try{
// 1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2.获得连接
conn = DriverManager.getConnection("jdbc:mysql:///day03", "root", "123");
// 3.创建执行SQL对象,执行SQL
stmt = conn.createStatement();
String sql = "insert into user values (null,'fff','abc','刘xx',3800)";
int num = stmt.executeUpdate(sql);
if(num > 0){
System.out.println("保存成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
// 4.释放资源
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
}
03预处理SQL语句(常用以上了解)PreparedStatement 关键类
/**
解决了SQL注入漏洞的登录
-
@author jt
*/
public class UserDao2 {public boolean login(String username,String password){ boolean flag = false; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try{ // 获得连接: conn = JDBCUtils.getConnection(); // 编写SQL: String sql = "select * from user where username = ? and password = ?"; // 预处理sql: pstmt = conn.prepareStatement(sql); // 设置参数 pstmt.setString(1, username); pstmt.setString(2, password); // 执行sql: rs = pstmt.executeQuery(); if(rs.next()){ flag = true; } }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(rs, pstmt, conn); } return flag; } }
04PreparedStatement的CRUD的操作 (关键)
/**
* PreparedStatement的CRUD的操作
* @author jt
*
*/
public class JDBCDemo1 {
@Test
// 查询一条
public void demo4(){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
User user = new User();
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写sql:
String sql = "select * from user where id = ?";
// 预编译sql
pstmt = conn.prepareStatement(sql);
// 设置参数
pstmt.setInt(1, 2);
// 执行sql:
rs = pstmt.executeQuery();
if(rs.next()){
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setNickname(rs.getString("nickname"));
user.setSalary(rs.getDouble("salary"));;
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(rs, pstmt, conn);
}
System.out.println(user);
}
@Test
// 删除操作
public void demo3(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写SQL:
String sql = "delete from user where id=?";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
// 设置参数:
pstmt.setInt(1, 7);
// 执行SQL:
int num = pstmt.executeUpdate();
if(num > 0){
System.out.println("删除成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release( pstmt, conn);
}
}
@Test
// 修改操作
public void demo2(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写SQL:
String sql = "update user set username =?,password=?,nickname=?,salary=? where id=?";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
// 设置参数:
pstmt.setString(1, "abc");
pstmt.setString(2, "yui");
pstmt.setString(3, "王老八");
pstmt.setDouble(4, 6600);
pstmt.setInt(5, 7);
// 执行SQL:
int num = pstmt.executeUpdate();
if(num > 0){
System.out.println("修改成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release( pstmt, conn);
}
}
@Test
// 保存操作
public void demo1(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写SQL:
String sql = "insert into user values (null,?,?,?,?)";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
// 设置参数:
pstmt.setString(1, "hhh");
pstmt.setString(2, "123456");
pstmt.setString(3, "王老五");
pstmt.setDouble(4, 9000);
// 执行SQL:
int num = pstmt.executeUpdate();
if(num > 0){
System.out.println("保存成功!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(pstmt, conn);
}
}
}