DriverManager(驱动管理类)
Drivermanager的作用有:
1.注册驱动;
2.获取数据库连接
Class.forName("com.mysql.cj.jdbc.Driver");
这一行的作用就是注册Mysql驱动(把我们下载的jar包加载到内存里去)
常用的方法
这个方法用于与数据库建立链接,参数分别是数据库的地址,用户名,用户的密码;
public static void getConnection() {
try {
// 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println("找不到MySQL驱动程序类!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("获取数据库连接时出现错误!");
e.printStackTrace();
}
}
Connection(数据库连接对象)
Connection的作用:
1.获取执行SQL的对象
2.管理事务
常用的方法
Statement
Statement唯一的作用就是用来执行SQL语句,在使用是还有被恶意SQL注入的风险。
常用的方法
executeUpdate(sql):用来执行增、删和修改的操作;
executeQuery(sql):用来执行查询的操作,查询的结果借助ResultSet对象来访问;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Scanner;
public class DataBase {
static final String url = "jdbc:mysql://localhost:3306/chat";
static final String user = "root";
static final String password = "123456";
static Connection connection = null;
// 获取数据库连接
public static void getConnection() {
try {
// 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");//增删查改 jdbc er图
// 获取连接
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
System.out.println("找不到MySQL驱动程序类!");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("获取数据库连接时出现错误!");
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
getConnection();
//add();
//print();
//change();
//delete();
connection.close();
}
public static void change() throws SQLException {
Statement st = connection.createStatement();
st.executeUpdate("UPDATE users SET username=7,password=8,nickname=9");
st.close();
}
public static void print() throws SQLException {
Statement st = connection.createStatement();
ResultSet s = st.executeQuery("SELECT ID,USERNAME,PASSWORD,NICKNAME FROM USERS");
while (s.next()) {
int id = s.getInt("id");
String username = s.getString("username");
String password = s.getString("password");
String nickname = s.getString("nickname");
System.out.println("id:" + id + "\t" + "username:" + username + "\t" + "password:" + password + "\t" + "nickname:" + nickname);
}
st.close();
s.close();
}
public static void add() throws SQLException {
Statement st = connection.createStatement();
st.executeUpdate("INSERT INTO users(id,username,password,nickname) VALUES(5,5,5,5)");
st.close();
}
public static void delete() throws SQLException {
Statement st = connection.createStatement();
st.executeUpdate("DELETE FROM users WHERE ID=5");
st.close();
}
}
ResultSet(结果对象集)
ResultSet封装了查询语句的结果。
getXxx(参数):参数是你需要的数据表的字段
注意:要获取数据的时候要先调用next移动光标才能获取数据
PreparedStatement(预编译)
PreparedStatement是Statement的升级版作用是预编译SQL语句并执行,防止恶意的SQL注入;
使用方法:
1.获取PreparedStatement对象
2.设置参数值
3.执行SQL语句
public int Add(Connection connection) throws SQLException {
if(Find(connection)==1){
return 0;
}
PreparedStatement preparedStatement =
connection.prepareStatement("INSERT INTO STUDENTS(ID,PASSWORD,MAIL,NICKNAME,NAME,IDENTITY) " +
"VALUES (?,?,?,?,?,?)");
preparedStatement.setString(1,getId());
preparedStatement.setString(2,getPassword());
preparedStatement.setString(3,getMail());
preparedStatement.setString(4,getNickname());
preparedStatement.setString(5,getName());
preparedStatement.setInt(6,getIdentity());
preparedStatement.executeUpdate();
preparedStatement.close();
return 1;
}