第一步:注册驱动(告诉Java程序连接的是哪个品牌的数据库)
第二步:获取连接(表示JVM的进程和数据库进程之间的通道打开了,属于进程之间的通信,使用完之后要关闭)
第三步:获取数据库操作对象(专门执行sql语句的对象)
第四步:执行SQL语句(DQL DML…)
第五步:处理查询结果集(只有第四步执行的是select语句的时候,才有第五步处理查询结果集)
第六步:释放资源(使用完资源之后要关闭资源。Java和数据库属于进程间的通信,开启之后要关闭。)
URL:统一资源定位符,包括通信协议(提前定好的数据传送格式),IP地址,数据库端口,服务器上的某个资源名
MySQL的URL:jddbc:mysql://localhost:3306/bjpoewernode
Oracle的URL:jdbc:oracle:thin:@localhost:1521:orcl
public class JDBCTest01 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1.注册驱动
//第一种写法
Driver driver = new com.mysql.jdbc.Driver();//多态,父类型引用指向子类型对象
//oracle的驱动 Driver driver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(driver);
//第二种写法,更常用(参数是字符串,可以写到配置文件中)
Class.forNmae("com.mysql.jdbc.Driver");//不需要接收返回值,因为只想用它的类加载动作
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/bjpowernode";
String user="root";
String password = "333";
conn = DriverManager.getConnection(url,user,password);
//3.获取数据库操作对象
stmt = conn.createStatement();
//4.执行sql
//.executeUpdate执行DML语句(insert delete update),返回值是影响数据库中的记录条数
String sql = "insert into dept(deptno,dname,loc) values (50,'人事部','北京')";//JDBC中sql语句不需要提供分号结尾
int count = stmt.executeUpdate(sql);
System.out.println(count==1?"保存成功":"保存失败");
//5.处理查询结果集
} catch (SQLException e) {
e.printStackTrace();
}finally {
//6.释放资源,从小到大
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
使用资源绑定属性配置文件
//jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/bjpowernode
username=root
password=333
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
...
Class.forNmae(driver);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
public class JDBCTest05 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/bjpowernode";
String user = "root";
String password = "333";
conn = DriverManager.getConnection(url, user, password);
//3.获取数据库操作对象
stmt = conn.createStatement();
//4.执行sql
String sql = "select empno,ename,sal from emp";
rs = stmt.executeQuery(sql);//专门执行DQL语句的方法
//5.处理查询结果集
while (rs.next()) {
//以列的下标获取
String empno = rs.getString(1);
String ename = rs.getString(2);
String sal = rs.getString(3);
System.out.println(empno + "," + ename + "," + sal);
//以查询结果集的列名称获取
String empno2 = rs.getString("empno");
String ename2 = rs.getString("ename");
String sal2 = rs.getString("sal");
//以特定的类型取出
int empno3 = rs.getInt("empno");
String ename3 = rs.getString("ename");
double sal3 = rs.getDouble("sal");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//6.释放资源,从小到大
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
实现用户登录
public class JDBCTest07 {
public static void main(String[] args) {
//初始化一个界面
Map<String, String> userLoginInfo = initUI();
//验证用户名和密码
boolean loginScuess = login(userLoginInfo);
System.out.println(loginScuess == true ? "登录成功" : "登录失败");
}
private static boolean login(Map<String, String> userLoginInfo) {
boolean loginSuccess = false;
String loginName = userLoginInfo.get("loginName");
String loginPwd = userLoginInfo.get("loginPwd");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://loaclhoat:3306/bjpowernode", "root", "333");
stmt = conn.createStatement();
String sql = "select*from t_user where loginName = '" + loginName + "' and loginPwd = '" + loginPwd + "'";
rs = stmt.executeQuery(sql);
if (rs.next()) loginSuccess = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return loginSuccess;
}
private static Map<String, String> initUI() {
Scanner s = new Scanner(System.in);
System.out.println("用户名");
String loginName = s.nextLine();
System.out.println("密码");
String loginPwd = s.nextLine();
Map<String, String> userLoginInfo = new HashMap<>();
userLoginInfo.put("loginName", loginName);
userLoginInfo.put("loginPwd", loginPwd);
return userLoginInfo;
}
}
上一章:JDBC 第一章 JDBC概述
下一章:JDBC 第三章 SQL注入