【Java】JavaJDBC(快速理解)

1.JDBC是什么?

Java DataBase Connectivity(Java语言连接数据库) 

2.JDBC的本质是什么?

JDBC是SUN公司制定的一套接口(interface)

java.sql.*; (这个软件包下有很多接口。)

【Java】JavaJDBC(快速理解)

3.JDBC开发前的准备工作,先从官网下载对应的驱动jar包,然后将其配置到环境变量classpath当中。

classpath=.;D:course06-JDBCresourcesMySql Connector Java 5.1.23mysql-connector-java-5.1.23-bin.jar

以上的配置是针对于文本编辑器的方式开发,使用IDEA工具的时候,不需要配置以上的环境变量。

IDEA有自己的配置方式。

4.JDBC编程六步(需要背会)

第一步:注册驱动(作用:告诉Java程序,即将要连接的是哪个品牌的数据库)

第二步:获取连接(表示JVM的进程和数据库进程之间的通道打开了,这属于进程之间的通信,重量级的,使用完之后一定要关闭通道。)

第三步:获取数据库操作对象(专门执行sql语句的对象)

第四步:执行SQL语句(DQL DML....)

第五步:处理查询结果集(只有当第四步执行的是select语句的时候,才有这第五步处理查询结果集。)

第六步:释放资源(使用完资源之后一定要关闭资源。Java和数据库属于进程间的通信,开启之后一定要关闭。)

import java.sql.Driver;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.Statement;

public class JDBCTest{

public static void main(String[] args)

{

Connection conn= null;

Statement stmt = null;

ResultSet rs = null;

try{

//1.注册驱动

//第一种方式

Driver driver = new com.mysql.jdbc.Driver();

DriverManager.registerDriver(driver);

//第二种方式(一下方法不需要返回值,我们只需要它的类加载动作)

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);

System.out.println("数据库连接对象:"+conn);

//[email protected]

//3.获取数据库操作对象(statement专门执行sql语句)

stmt = conn.createStatement();

//4.执行sql

String sql = "select empno as a,ename,sal from emp";

rs= stmt.executeQuery(sql); //executeUpdate执行DML语句 executeQuery(sql)执行DQL语句

//5.处理查询结果集

while(rs.next())

{

int empno = rs.getInt("a");

String ename = rs.getString("ename");

double sal = rs.getDouble("sal");

System.out.pritnln(empno+","+ename+","+sal);

}

}

catch(SQLException e){

e.printStackTrace();

}finally{

try{

if(stmt !=null){

stmt.close();

}

}catch(SQLException e){

e.printStackTrace();

}

try{

if(conn !=null){

conn.close();

}

}catch(SQLException e){

e.printStackTrace();

}

}

}

}

使用属性配置文件

//jdbc.propertes属性配置文件

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/bjpowernode

user=root

password=333

import java.sql.Driver;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.Statement;

import java.util.*;

public class JDBCTest{

public static void main(String[] args)

{

//使用资源绑定器绑定属性配置文件

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");

Connection conn= null;

Statement stmt = null;

try{

//1.注册驱动

class.forName(driver);

//2.获取连接

conn = DriverManager.getConnection(url,user,password);

System.out.println("数据库连接对象:"+conn);

//[email protected]

//3.获取数据库操作对象(statement专门执行sql语句)

stmt = conn.createStatement();

//4.执行sql

String sql = "insert into dept(deptno,dname,loc) values(50,'人事部','北京')";

int count = stmt.executeUpdate(sql); //执行DML语句

System.out.println(count == 1 ? "保存成功":"保存失败");

//5.处理查询结果集

}

catch(Exception e){

e.printStackTrace();

}finally{

try{

if(stmt !=null){

stmt.close();

}

}catch(SQLException e){

e.printStackTrace();

}

try{

if(conn !=null){

conn.close();

}

}catch(SQLException e){

e.printStackTrace();

}

}

}

}

5.登录功能实现(防止SQL注入,PreparedStatement)

package com.bjpowernode.jdbc;

import java.sql.*;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

/*实现功能: 1.需求:模拟用户登录功能的实现。 2.业务描述: 程序运行的时候,提供一个输入的入口,可以让用户输入用户名和密码 合法:显示登录成功。 不合法:显示登录失败。 3.数据准备: 在实际的开发中使用建模工具 4.Sql注入现象(安全隐患): 根本原因 fdsa ''or 将用户名或密码的or 当做了sql语句 解决SQL注入问题: 要想用户信息不参与Sql语句编译,那么必须使用java.sql.PreparedStatement PreparedStatement是属于预编译数据库操作对象,编译一次执行多次。 PreparedStatement ps = null; 其中一个?代表一个占位符,站位符不能用点引号括起来 String sql = "select * from t_user where loginName = ? and loginPwd = ?"; ps = conn.prepareStatement(sql); 给占位符传值 ps.setString(1,loginName); ps.setString(2,loginPwd); */

public class JDBCTest06 {

public static void main(String[] args)

{

//初始一个界面

Map<String,String> userLoginInfo = initUI();

boolean loginSuccess = login(userLoginInfo);

System.out.println(loginSuccess? "登录成功!":"登录失败用户名密码错误!");

}

private static boolean login(Map<String, String> userLoginInfo) {

boolean loginSuccess = false;

Connection conn = null;

PreparedStatement ps = null;//这里是PreParedStatement(预编译数据库操作对象)

ResultSet rs = null;

try{

//注册驱动

Class.forName("com.mysql.jdbc.Driver");

//连接语句

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bjpowernode","root","333");

//获取数据库操作对象

String sql = "select * from t_user where loginName = ? and loginPwd = ?";

ps = conn.prepareStatement(sql);

//给占位符传值

ps.setString(1,userLoginInfo.get("loginName"));

ps.setString(2,userLoginInfo.get("loginPwd"));

rs = ps.executeQuery();

if (rs.next())

{

loginSuccess = true;

}

}catch (ClassNotFoundException e){

e.printStackTrace();

}catch (SQLException e)

{

e.printStackTrace();

}finally {

if (rs !=null){

try {

rs.close();

}catch (SQLException e)

{

e.printStackTrace();

}

}

if (ps !=null){

try {

ps.close();

}catch (SQLException e)

{

e.printStackTrace();

}

}

if (conn !=null)

{

try{

conn.close();

}catch (SQLException e)

{

e.printStackTrace();

}

}

}

return loginSuccess;

}

/* * 初始化用户页面 * @return 用户输入用户名密码等登录信息 * */

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;

}

}

Statement的用处(需要SQL注入)

Statement存在SQL注入问题,是编译一次执行一次。PreparedStatement执行效率高。
需要先

用户台输入desc降序,输入asc升序,此时需要使用Sql注入

String keyWords = desc或者asc

String sql = "select ename = from emp order by ename " + keyWords;

6.账户转账演示事务

sql脚本

drop table if exists t_act;

create table t_act(

actno bigint,

balance double(7,2) //注意:7表示有效数字,2表示小数位个数

);

insert into t_act(actno,balance) values(111,20000);

insert into t_act(actno,balance) values(222,0);

commit;

select * from t_act;

重点三句:

conn.setAutoCommit(false);

conn.commit();

conn.rollback();

public class HelloWorld {

public static void main(String []args) {

Connection conn = null;

PreparedStatement ps = null;

try{

//注册驱动

Class.forName("com.mysql.jdbc.Driver");

//获取连接

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode","root","333");

//将自动提交机制改为手动提交

conn.setAutoCommit(false);//开启事务

//获取预编译的数据库操作系统

String sql = "update t_act set balance = ? where actno = ?";

ps = conn.prepareStatement(sql);

//给替换符赋值

ps.setDouble(1,10000);

ps.setInt(2,111);

int count = ps.executeUpdate();

ps.setDouble(1,10000);

ps.setInt(2,222);

count +=ps.executeUpdate();

//程序到这里表示成功

conn.commit(); //提交事务

Syste.out.println(count == 2 ?"转账成功":"转账成功");

}catch(Exception e)

{

if(conn != null)

{

try{

//回滚事务

conn.rollback();

}catch(SQLException e)

{

e.printStackTrace();

}

}

e.printStackTrace();

}

}

}

7.JDBC工具类封装

import java.sql.*;

public class DBUtil {

private DBUtil() {

}

//工具类的构造方法是私有的;

//因为工具类的方法是静态的,不需要实例化

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

//获取数据库连接对象

//@return 连接对象

public static PreparedStatement createStatement(String sql) throws SQLException {

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "333");

PreparedStatement ps = conn.prepareStatement(sql);

return ps;

}

//关闭资源

//conn 连接对象

//ps 数据库操作对象

//rs 结果集

public static void close(Connection conn, Statement ps, ResultSet rs) {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

java

以上是 【Java】JavaJDBC(快速理解) 的全部内容, 来源链接: utcz.com/a/96374.html

回到顶部