这里需提前将maven环境配置好
链接: Maven环境配置
Java Database Connectivity(java语言连接数据库)
JDBC本质----接口
接口都有调用者和实现者
面向接口调用,面向接口写实现类
2.仓库中获取mysql的驱动依赖
从https://mvnrepository.com/ 仓库中获取mysql的驱动依赖
mysql
mysql-connector-java
8.0.29
1.Class.forName()加载驱动
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
2.DriverManager获取Connection连接
// 创建连接 有三个参数 第一个url 第二个 user 第三个 password
// url : 数据的地址 协议:什么样的数据库:数据库服务器的地址:端口号/数据库名称
// user : 数据库的用户名
//password : 数据库的密码
//地址还可以添加参数 &useUnicode=true&characterEncoding=utf-8 为设置字符集为utf-8 专门针对乱码
//useSSL=false 是否选择加密连接
//获取connection连接
connection = DriverManager.getConnection(
"jdbc:mysql://192.168.95.130:3306/jdbcstu",
"root",
"root"
);
System.out.println(connection);
3.创建Statement执行SQL语句
/*新增*/
String sql ="insert into master(name,age,gender,yearnum,did) value(?,?,?,?,?)";
String name ="张三";
Integer age =20;
String gender="男";
Integer yearnum=3;
Integer did =1;
//创建preparedStatement---防止SQL注入
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setInt(2,age);
preparedStatement.setString(3,gender);
preparedStatement.setInt(4,yearnum);
preparedStatement.setInt(5,did);
int num = preparedStatement.executeUpdate();
if (num>0){
System.out.println("新增主人信息成功");
}
/*修改*/
String sql ="update master set age =? where pid=?";
Integer age = 18;
Integer pid =2;
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setInt(1,age);
preparedStatement.setInt(2,pid);
int num =preparedStatement.executeUpdate();
if (num>0){
System.out.println("修改主人信息成功");
}
/*删除*/
String sql="delete from master where pid =?";
preparedStatement =connection.prepareStatement(sql);
Integer pid=4;
preparedStatement.setInt(1,pid);
int num = preparedStatement.executeUpdate();
if (num>0){
System.out.println("删除主人信息成功");
}
4.返回ResultSet查询结果()
只有执行select语句才有查询结果集
/*查询*/
String sql ="select pid ,m.name,age,gender,yearnum,d.name,health,love,strain,lytm from master m,dog d where m.did=d.id ";
preparedStatement =connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
System.out.println("\t\t信息表");
System.out.println("编号\t名字\t年龄\t性别\t经验值\t宠物名字\t\t健康值\t亲密度\t品种\t领养时间");
while (resultSet.next()){
System.out.print(resultSet.getInt("pid")+"\t"); System.out.print(resultSet.getString("m.name")+"\t");
System.out.print(resultSet.getInt("age")+"\t"); System.out.print(resultSet.getString("gender")+"\t"); System.out.print(resultSet.getInt("yearnum")+"\t\t"); System.out.print(resultSet.getString("d.name")+"\t\t");
System.out.print(resultSet.getInt("health")+"\t\t");
System.out.print(resultSet.getInt("love")+"\t\t"); System.out.print(resultSet.getString("strain")+"\t\t");
System.out.println(resultSet.getDate("lytm")+"\t");
}
5.释放资源
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}