1.数据库连接方式1:java程序要操作mysql数据库要先得到connect连接。使用反射加载Driver类,动态加载,更加的灵活,减少依赖性。
public void connect01() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/fu_db03";
//将用户名和密码放入到Properties对象
Properties properties = new Properties();
properties.setProperty("user", "root"); //用户
properties.setProperty("password", "root"); //密码
Connection connect = driver.connect(url, properties);
System.out.println("方式一=" + connect);
}
2.使用DriverManager 代替 Driver 进行统一管理。管理一组 JDBC 驱动程序的基本服务。registerDriver()方法向DriverManager注册给定的驱动程序。
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/fu_db03";
String user = "root";
String password = "root";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式二=" + connection);
}
3.使用Class.forName在加载Driver类时,自动完成注册驱动。在底层有new Driver(),因此注册driver的已经完成。
public void connect03() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/fu_db03";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式三=" + connection);
}
4.将数据库连接需要的4个基本信息声明在配置文件中,通过读取配置文件的方式,获取连接,让连接mysql更加灵活。
配置文件:
user=root password=root url=jdbc:mysql://localhost:3306/fu_db03 driver=com.mysql.jdbc.Driver
public void connect04() throws IOException, ClassNotFoundException, SQLException {
//通过Properties对象获取配置文件的信息
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("方式四=" + connection);
}
5.Statement对象用于执行静态SQL语句并返回其生成的结果的对象。在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过Statement、PreparedStatement、CallableStatement。Statement对象执行SQL语句,存在SQL注入风险。SQL注入是利用某些系统没有对用户输入数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库。要防范SQL注入,只要用PreparedStatement取代Statement就可以了。CREATE TABLE admin(
`name` VARCHAR(32) NOT NULL UNIQUE,
pwd VARCHAR(32) NOT NULL DEFAULT '')
CHARACTER SET utf8;
INSERT INTO admin VALUES('tom','123');
SELECT * FROM admin
WHERE `name` = 'tom' AND pwd = '123';
-- SQL注入
-- 输入用户名 为 1' or
-- 输入万能密码 为 or '1'= '1
SELECT * FROM admin
WHERE `name` = '1' OR' AND pwd = 'OR '1'= '1';
本文介绍了Java连接MySQL数据库的四种方法,包括使用反射加载Driver、DriverManager注册驱动、Class.forName自动注册以及从配置文件读取信息。同时,讨论了Statement对象在执行SQL语句时存在的SQL注入风险,并提出使用PreparedStatement来防止SQL注入。文章旨在展示如何更安全、灵活地管理数据库连接。
1871

被折叠的 条评论
为什么被折叠?



