JDBC连接数据库
1.加载驱动
2.获取连接对象(java程序 192.168.153.1 Connection)-------mysql()
3.Statement 承载执行SQL语句,并执行(Statement 是connection创建出来)
4.数据库返回sql执行的结果
5.释放资源
添加mysql.8.0.29
找到pom.xml文件,找到<dependency>添加
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency>
添加后右上角M刷新加载
一、连接驱动
找到App.java文件
public static void main( String[] args )
{
Connection connection =null;
PreparedStatement preparedStatement =null;
String url = "jdbc:mysql://192.168.153.149:3306/pet";//定义一个访问路径
String user ="root";//用户
String pwd ="root";//密码
// 1. 加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取Connection对象
connection = DriverManager.getConnection(url,user,pwd); System.out.println(connection);
// 创建dog表
create table dog(
id int auto_increment primary key ,
name varchar(32),
health int,
love int,
strain varchar(32),
`datetime` datetime
);
// 新增一条狗
String name ="常威";
String strain ="藏獒";
int love=100;
int health=100;
Date date = new Date(1623313749000l);
Time time = new Time(1623313749000l);
Timestamp timestamp = new Timestamp(1623313749000l);
String insertSql="insert into dog(name,health,love,strain,datetime)" +
" values(?,?,?,?,?)";
// 3.Statement 承载执行SQL语句,并执行
preparedStatement = connection.prepareStatement(insertSql);
preparedStatement.setString(1,name);
preparedStatement.setInt(2,health);
preparedStatement.setInt(3,love);
preparedStatement.setString(4,strain);
/*preparedStatement.setDate(5,date);*/
/*preparedStatement.setTime(5,time);
preparedStatement.setTimestamp(5,timestamp);
int num = preparedStatement.executeUpdate();
if(num>0)
System.out.println("新增成功!");
//删除数据
/* String deleteStr = "delete from dog where id=?";
preparedStatement = connection.prepareStatement(deleteStr );
preparedStatement.setInt(1,3);
int num = prepareStatement.executeUpdate();
if(num>0)
System.out.println("新增成功!");
*/
//修改数据
/*String updateStr = "update dog set name=?,health=? where=?";
preparedStatement = connection.prepareStatement(updateStr);
preparedStatement.setString(1,"来福");
preparedStatement.setInt(2,0);
preparedStatement.setInt(3,4);
int num = preparedStatement.executeUpdate();
if (num>0)
System.out.println("修改成功");*/
//4.数据库返回sql执行的结果 resultSet(一个返回结果集)
//查询
String queryStr="select id,name,health,love,strain,datetime from dog";
preparedStatement = connection.prepareStatement(queryStr);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
System.out.println(resultSet.getInt("id")+"\t"
+ resultSet.getString("name")+"\t"
+resultSet.getInt("health")+"\t"
+resultSet.getInt("love")+"\t"
+resultSet.getString("strain")+"\t"
+resultSet.getString("datetime")+"\t"
+resultSet.getTimestamp("datetime")+"\t"
);
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
if (preparedStatement!=null) {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
该文详细介绍了如何通过Java的JDBCAPI连接到MySQL数据库,包括加载驱动、建立连接、创建Statement和PreparedStatement对象执行SQL操作,以及处理结果集和释放资源的过程。示例代码中展示了创建表、插入数据、查询数据等基本操作。

被折叠的 条评论
为什么被折叠?



