package com.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 第一种连接方式
*/
public class jdbc01 {
public static void main(String[] args) throws SQLException {
//1.注册驱动
Driver driver = new Driver();
//2.得到连接
//① jdbc:mysql:// 规定好的表示协议,通过jdbc的方式连接mysql
//② localhost: 主机,可以是ip地址
//③ 3306 表示mysql监听的端口
//④ execute 连接到mysql dbms的哪个数据库
//⑤ mysql的连接本质就是前面学过的socket连接
String url = "jdbc:mysql://localhost:3306/execute";
//将用户名和密码放入到Properties对象中
Properties properties = new Properties();
//说明:user和password是规定好,后面的值根据实际情况写
properties.setProperty("user","root"); //用户
properties.setProperty("password","178966");//密码
//用mysql的驱动输入的用户和密码去连接
Connection connect = driver.connect(url, properties);
//3.执行sql
// String sql = "insert into actor values(null,'刘德华','男','1970-11-11','110')";
String sql = "update actor set name = '杨佳涵' where id = 1";
//statement 用户执行静态SQL语句并返回其生成的结果的对象
Statement statement = connect.createStatement();
int rows = statement.executeUpdate(sql);
System.out.println(rows > 0 ? "成功" : "失败");
//4.关闭连接资源
statement.close();
connect.close();
}
}
package com.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* 第二种连接方式: 使用反射加载Driver类,动态记载,更加的灵活,减少依赖性
*/
public class jdbc02 {
public static void main(String[] args) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//1.注册驱动
//使用反射加载Driver类,动态加载,更加的灵活,减少依赖性
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//得到连接
String url = "jdbc:mysql://localhost:3306/execute";
//将用户名和密码放入Properties对象中
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","178966");
Connection connection = driver.connect(url,properties);
//3.操作SQL语句
String sql = "delete from actor where id = 2";
//Statement用于执行静态SQL语句并返回其生成的结果的对象
Statement statement = connection.createStatement();
int rows = statement.executeUpdate(sql); //如果是 dml语句,返回的就是受影响的行数
System.out.println(rows > 0 ? "成功" : "失败");
//关闭资源连接
statement.close();
connection.close();
}
}
package com.jdbc;
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 第三种连接数据库方式
*/
public class jdbc03 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
//使用反射加载Driver
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
//创建url和user和password
String url = "jdbc:mysql://localhost:3306/execute";
String user = "root";
String password = "178966";
DriverManager.registerDriver(driver); //注册Driver驱动
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
}
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 创建连接的第四种方式
*/
public class jdbc04 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//使用反射加载了Driver类
Class.forName("com.mysql.jdbc.Driver");
//创建url和user和password
String url = "jdbc:mysql://localhost:3306/execute";
String user = "root";
String password = "178966";
Connection connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
}
}
package com.jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 创建连接的第五种方式:最常用
*/
public class jdbc05 {
public static void main(String[] args) 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);
}
}
SQL注入:
name = ‘1’ or’ and pwd = 'or ‘1’ = ‘1’
输入用户名:1’ or
输入密码为:or ‘1’ = '1