一、JDBC介绍
二、JDBC 快速入门
package com.itheima.jdbc;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;public class JDCBDemo {public static void main(String[] args) throws Exception {//1、注册驱动Class.forName("com.msql.jdbc.Driver");//2、获取链接String url = "jdbc:mysql://127.0.0.1:3306/db1";String username = "root";String password = "123456";Connection conn = DriverManager.getConnection(url, username, password);//3、定义sqlString sql = "update account set money = 2000 where id = 1";//4、获取执行sql的对象 StatementStatement stmt = conn.createStatement();//5、执行sqlint count = stmt.executeUpdate(sql);//受影响的行数//6、处理结果System.out.println(count);//7、释放资源stmt.close();conn.close();}
}
三、JDBC API详解
1、DriverManager
2、Connection
//3、定义sqlString sql1 = "update account set money = 3000 where id = 1";String sql2 = "update account set money = 3000 where id = 2";Statement stmt = conn.createStatement();try {//开启事物conn.setAutoCommit(false);int count1 = stmt.executeUpdate(sql1);System.out.println(count1);int count2 = stmt.executeUpdate(sql2);System.out.println(count2);//提交事物conn.commit();} catch (Exception throwables) {//回滚事物conn.rollback();throwables.printStackTrace();}//7、释放资源stmt.close();conn.close();
3、Statement
package com.itheima.jdbc;import java.sql.*;public class JDCBDemo {public static void main(String[] args) throws Exception {//1、注册驱动Class.forName("com.msql.jdbc.Driver");//2、获取链接String url = "jdbc:mysql://127.0.0.1:3306/db1";String username = "root";String password = "123456";Connection conn = DriverManager.getConnection(url, username, password);//3、定义sqlString sql = "select * from account";//4、获取statememt对象Statement stmt = conn.createStatement();//5、执行sqlResultSet rs = stmt.executeQuery(sql);//6、处理结果,遍历rs中的所有数据//6.1 光标向下移动一行,并且判断当前是否有数据while (rs.next()){//6.2 获取数据 getXxxint id = rs.getInt(1);String name = rs.getString(2);double money = rs.getDouble(3);System.out.println(id);System.out.println(name);System.out.println(money);System.out.println("----------------------");}//7、释放资源rs.close();stmt.close();conn.close();}
}
4、PrepareStatement
package com.itheima.jdbc;import java.sql.*;public class JDCBDemo {public static void main(String[] args) throws Exception {//1、注册驱动Class.forName("com.msql.jdbc.Driver");//2、获取链接String url = "jdbc:mysql://127.0.0.1:3306/db1";String username = "root";String password = "123456";Connection conn = DriverManager.getConnection(url, username, password);//接受用户输入 用户名和密码String name = "zhangsan";String pwd = "123";String sql = "select * from tb_user where username = '" + name + "' and password = '" + pwd + "'";//获取stmt对象Statement stmt = conn.createStatement();//执行sqlResultSet rs = stmt.executeQuery(sql);//判断是否登录成功if (rs.next()){System.out.println("登录成功");}else {System.out.println("登录失败");}//释放资源rs.close();stmt.close();conn.close();}
}
package com.itheima.jdbc;import java.sql.*;public class JDCBDemo {public static void main(String[] args) throws Exception {//1、注册驱动Class.forName("com.msql.jdbc.Driver");//2、获取链接String url = "jdbc:mysql://127.0.0.1:3306/db1";String username = "root";String password = "123456";Connection conn = DriverManager.getConnection(url, username, password);//接受用户输入 用户名和密码String name = "zhangsan";String pwd = "123";//定义sqlString sql = "select * from tb_user where username = ? and password = ?";//获取pstmt对象PreparedStatement pstmt = conn.prepareStatement(sql);//设置 ? 的值pstmt.setString(1,name);pstmt.setString(2,pwd);//执行sqlResultSet rs = pstmt.executeQuery();//判断是否登录成功if (rs.next()){System.out.println("登录成功");}else {System.out.println("登录失败");}//释放资源rs.close();pstmt.close();conn.close();}
}