在使用JDBC连接数据库之前,首先要有数据库,数据库要创建表。我的数据库信息如下:
create table student(
id int primary key auto_increment,
name varchar(20),
age int
);
mysql
mysql-connector-java
6.0.6
public static Connection getConnection() {
try {
// 1.通过DriverManger注册驱动,注意此时Driver是在com.mysql.jdbc包中
DriverManager.registerDriver(new Driver());
/**
* 2.通过DriverManager获取连接对象
*
* jdbc:mysql://:这是固定的写法,表示使用jdbc连接mysql数据库
* localhost:ip地址,本地可以写成localhost。
* 3306:mysql的端口号。
* xia:数据库的名字。
* 第一个root:mysql的用户名
* 第二个root:mysql的密码。
*/
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xia", "root", "root");
return connection;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static boolean insertStudent1(Student student) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,获取需要传递的参数
String name = student.getName();
int age = student.getAge();
//4,拼接sql语句
String sql = "insert into student (name,age) VALUE ('" + name + "'," + age + ")";
//5,执行sql语句
statement.execute(sql);
//6,释放资源
statement.close();
connection.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
此时就可以向数据库中插入数据,测试代码如下:
Student student = new Student();
student.setName("lilei");
student.setAge(18);
insertStudent1(student);
注:Student是自定义的Model类,此时Student.java文件的代码略。
public static boolean insertStudent2(Student student) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,获取需要传递的参数
String name = student.getName();
int age = student.getAge();
//4,写sql语句,参数使用?占位符
String sql = "insert into student (name,age) VALUE (?,?)";
//5,得到PreparedStatement对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//6,通过PreparedStatement对象设置参数
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
//7,执行sql语句
preparedStatement.execute();
//8,释放资源
statement.close();
connection.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static Student selectStudent(int id) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,拼接sql语句
String sql = "select id,name,age from student where id = " + id;
//4,查询,返回的结果放入ResultSet对象中。
ResultSet resultSet = statement.executeQuery(sql);
//5,将游标后移一位
resultSet.next();
//6,获取数据
int studentId = resultSet.getInt(1);//第一行的第一列数据,我们知道是id,也知道是int类型,
String studentName = resultSet.getString(2);//第二个数据对应name
int studentAge = resultSet.getInt(3);//第三个数据对应age
Student student = new Student();
student.setId(studentId);
student.setName(studentName);
student.setAge(studentAge);
//7,释放资源
statement.close();
connection.close();
return student;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static List selectUserList(int age) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,拼接sql语句
String sql = "select id,name,age from student where age = " + age;
//4,查询,返回的结果放入ResultSet对象中。
ResultSet resultSet = statement.executeQuery(sql);
// 5.得到返回的值
List studentList = new ArrayList<>();
while (resultSet.next()) {//resultSet对象可能包含很多行数据,所以要是有while循环。
int studentId = resultSet.getInt(1);//第一行的第一列数据,我们知道是id,也知道是int类型,
String studentName = resultSet.getString(2);//第二个数据对应name
int studentAge = resultSet.getInt(3);//第三个数据对应age
Student student = new Student();
student.setId(studentId);
student.setName(studentName);
student.setAge(studentAge);
studentList.add(student);
}
//6,释放资源
statement.close();
connection.close();
return studentList;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static boolean updateStudent(Student student) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,获取需要传递的参数
String name = student.getName();
int id = student.getId();
//4,拼接sql语句
String sql = "update student set name = '"+name+"' WHERE id = " +id;
//5,执行sql语句
statement.executeUpdate(sql);
//6,释放资源
statement.close();
connection.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static boolean deleteStudent(int id) {
try {
//1,得到Connection对象,
Connection connection = getConnection();
//2,通过Connection获取一个操作sql语句的对象Statement
Statement statement = connection.createStatement();
//3,拼接sql语句
String sql = "DELETE FROM student WHERE id = " +id;
//4,执行sql语句
statement.execute(sql);
//5,释放资源
statement.close();
connection.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
一,使用JDBC进行增删改查操作大体相同,不同点就是sql语句。大体步骤如下:
二,使用JDBC也有缺点,即每次操作都要创建连接,然后关闭连接创,建连接和关闭连接是非常消耗资源的操作。所以在大型项目中尽量不要采用这种方式。