MySQL连接数据库
jar包的下载
要先使用java连接数据库,需要使用对应的jar包。jar包如何下载呢?
- 首先进入mysql官网:MySQL
- 进入官网后找到下载按钮,点进去
-
找到Connector/J,再点进去
- 找到下面这个按钮,继续点进去
- 找到自己需要的jar包就可以下载了
- 下载之后解压,在自己的项目中创建一个lib文件夹,把jar包放进去
- 右键选择该jar包,点击
之后我们就可以使用这个jar包与mysql数据库建立连接了
建立连接的步骤
- 加载驱动:两种方式,第一种使用DriverManager.registerDriver(new Driver()),第二种使用反射的方式Class.forName(“com.mysql.jdbc.Driver”)
//方式1
DriverManager.registerDriver(new Driver());
//方式2
Class.forName("com.mysql.jdbc.Driver");
- 建立连接:
//建立连接
Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.31.100:3306/demo?useSSL=false", "root", "123456");
- 创建Statement对象
//获取Statement对象
Statement statement = connection.createStatement();
- 编写sql语句
//编写sql语句
String sql="insert into Student (Sid,Sname,Sage,Ssex) values (20,'yz','2000-10-10 00:00:00','男')";
- 执行sql:当执行插入、删除等更新数据库的语句时使用executeUpdate、使用查询语句时使用executeQuery
//执行sql语句
statement.executeUpdate(sql);
当执行查询语句时,会返回一个ResultSet,该对象类似于迭代器,有next方法判断下一个数据是否为空,不为空返回true,也可以通过该对象通过get对应的数据类型(“字段名”)的方式获取查询所得的数据。
ResultSet resultSet = statement.executeQuery(selectSql);
while (resultSet.next()){
System.out.println(resultSet.getInt("Sid"));
System.out.println(resultSet.getString("Sname"));
System.out.println(resultSet.getString("Ssex"));
System.out.println(resultSet.getDate("Sage"));
}
- 关闭连接
//关闭连接
statement.close();
connection.close();
sql注入的问题
例如:
public class UserLogin {
/*
模拟用户的注册和登录
*/
static Scanner scanner = new Scanner(System.in);
static Connection connection;
static Statement statement;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
try {
connection = DriverManager.getConnection("jdbc:mysql://192.168.31.100:3306/demo?useSSL=false", "root", "123456");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws SQLException {
System.out.println("请选择你的选项 1.登录 2.注册");
if (scanner.hasNextInt()) {
int choice = scanner.nextInt();
if (choice == 1) {
login();
} else {
register();
}
}
connection.close();
}
public static void login() throws SQLException {
System.out.println("======登录界面=======");
String name = null;
String pwd = null;
System.out.println("输入姓名");
if (scanner.hasNext()) {
name = scanner.next();
}
System.out.println("输入密码");
if (scanner.hasNext()) {
pwd = scanner.next();
}
statement = connection.createStatement();
String sql = "select count(*) as cnt from user where name='" + name + "'and pwd='" + pwd + "'";
System.out.println(sql);
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()){
int cnt = resultSet.getInt("cnt");
if (cnt==1){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
statement.close();
/*
编写sql
*/
}
public static void register() throws SQLException {
System.out.println("=====注册界面======");
String name = null;
String pwd = null;
System.out.println("输入姓名");
if (scanner.hasNext()) {
name = scanner.next();
}
System.out.println("输入密码");
if (scanner.hasNext()) {
pwd = scanner.next();
}
statement = connection.createStatement();
String sql = "insert into user (name,pwd) values ('" + name + "','" + pwd + "')";
System.out.println(sql);
int i = statement.executeUpdate(sql);
System.out.println("执行结果" + i);
if (i > 0) {
System.out.println("注册成功");
} else {
System.out.println("注册失败");
}
statement.close();
}
}
上面的代码在传入密码时,加上’or’1’='1,即使密码错误也会返回结果,如何解决呢?
两种方法:1.使用hash函数。2.使用预编译
预编译的实现方式:先编写sql时对传入的字段值使用 ?占位,在使用connection创建对象时,不要创建Statement,而是创建一个PreparedStatement对象,之后把sql传入PreparedStatement对象进行预编译。之后再对?占位的地方使用preparedStatement.setString()的方式赋值,注意:这里的传值可以根据 ?的顺序传值,下标从1开始
对上述代码的优化如下:
public class UserloginPlus {
/*
对于上一个程序,若在传入密码时,加上'or'1'='1,则会导致sql注入
解决方法:
1.使用hash
2.使用预编译
*/
static Scanner scanner = new Scanner(System.in);
static Connection connection;
static Statement statement;
static {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
//建立连接
try {
connection = DriverManager.getConnection("jdbc:mysql://192.168.31.100:3306/demo?useSSL=false", "root", "123456");
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws SQLException {
System.out.println("请选择你的选项 1.登录 2.注册");
if (scanner.hasNextInt()) {
int choice = scanner.nextInt();
if (choice == 1){
login();
}else {
register();
}
}
}
public static void login() throws SQLException {
System.out.println("======登录界面=======");
String name = null;
String pwd = null;
System.out.println("输入姓名");
if (scanner.hasNext()) {
name = scanner.next();
}
System.out.println("输入密码");
if (scanner.hasNext()) {
pwd = scanner.next();
}
// statement = connection.createStatement();
//对于需要添加的值,需要用?占位
String sql = "select count(*) as cnt from user where name=? and pwd=?";
//得到一个PreparedStatement对象,该对象有方法可以对sql中的?进行传值
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//setString,方法可以传入索引和值,索引下标从1开始
preparedStatement.setString(1,name);
preparedStatement.setString(2,pwd);
//preparedStatement对象也有 statement的方法executeQuery等
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
int res = resultSet.getInt("res");
if (res == 1) {
System.out.println("登录成功...");
}else {
System.out.println("登录失败...");
}
// 如果有值,那么next为true
}
preparedStatement.close();
}
public static void register() throws SQLException {
String name = null;
String pwd = null;
System.out.println("输入姓名");
if (scanner.hasNext()) {
name = scanner.next();
}
System.out.println("密码");
if (scanner.hasNext()) {
pwd = scanner.next();
}
statement = connection.createStatement();
String sql = "insert into user (name,pwd) values ('" + name + "','" + pwd + "')";
System.out.println(sql);
int i = statement.executeUpdate(sql);
System.out.println("执行结果" + i);
if (i > 0) {
System.out.println("注册成功");
} else {
System.out.println("注册失败");
}
statement.close();
}
}