文章目录
1.准备connector驱动包
mysql版本应与connector驱动包版本对应
可在官网https://dev.mysql.com/downloads/j/选择对应的jar包下载就可以了
2.IDEA创建项目
创建目录,将jar包拷贝到目录中
可以Ctrl+c以及Ctrl+v将jar包拷贝进来
3. 连接数据库 进行插入操作
可概括为一下几步:
- 使用 DataSource 描述 MySQL 服务器的位置
- 和数据库服务器建立连接
- 构造 SQL 语句. JDBC 操作数据库, 本质仍然是通过 SQL 来描述数据库操作
- 执行 SQL 语句, insert, update, delete 都是通过 executeUpdate 来执行的
- 断开连接, 释放资源
3.1 使用 DataSource 描述 MySQL 服务器的位置.
DataSource dataSource = new MysqlDataSource();
设置数据库地址: 127.0.0.1 表示本机 IP ,3306 是端口号,text是数据库名,后面是字符集,SSL 是传输过程中是否加密。
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/text?characterEncoding=utf8&useSSL=false");
设置数据库用户名
大多是root
((MysqlDataSource)dataSource).setUser("root");
设置数据库的密码:
((MysqlDataSource)dataSource).setPassword("");
3.2 和数据库服务器建立连接
Connection connection = ((MysqlDataSource) dataSource).getConnection();
3.3 构造 SQL 语句. JDBC 操作数据库
构造sql语句
String sql = "insert into student values(1,'张三')";
通过 Connection 里面的 prepareStatement 方法,,把 String 类型的 sql语句转化为 JDBC 里面的对象:
PreparedStatement statement = connection.prepareStatement(sql);
3.4 执行 SQL 语句
insert, update, delete 都是通过 executeUpdate 来执行的
查询使用executeQuery
这里的 n 代表了表中有多少行收到了影响
int n = statement.executeUpdate();
3.5 断开连接, 释放资源
statement.close();
connection.close();
可见一行受到了影响
3.6自定义输入内容
int id = scanner.nextInt();
String name = scanner.next();
这两个 ? 就是表示当前字段的值还没有确定,先来占个位置,再使用 PreparedStatement 的 setXXX 系列方法进行替换:
这里的 1,2 就是下标,从 1 开始计算,把第一个 ?替换成 id 的值,把第二个 ?替换成 name 的值。然后打印一下替换的对象:
String sql = "insert into student values(?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
statement.setString(2,name);
4. 删除部分
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
//1.使用DateSource描述数据库的位置
DataSource datasource = new MysqlDataSource();
((MysqlDataSource)datasource).setUrl("jdbc:mysql://127.0.0.1:3306/text?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)datasource).setUser("root");
((MysqlDataSource)datasource).setPassword("");
//2.connection 和数据库建立连接
Connection connection = datasource.getConnection();
//3.构造sql语句
String sql ="delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
//4. 执行sql语句
int n = statement.executeUpdate();
System.out.println("n= "+n);
//5. 释放资源
connection.close();
statement.close();
}
5. 更新部分
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
//1.使用DateSource描述数据库的位置
DataSource datasource = new MysqlDataSource();
((MysqlDataSource)datasource).setUrl("jdbc:mysql://127.0.0.1:3306/text?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)datasource).setUser("root");
((MysqlDataSource)datasource).setPassword("");
//2.connection 和数据库建立连接
Connection connection = datasource.getConnection();
//3.构造sql语句
String sql ="delete from student where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1,id);
//4. 执行sql语句
int n = statement.executeUpdate();
System.out.println("n= "+n);
//5. 释放资源
connection.close();
statement.close();
}
6. 查询部分
public static void main(String[] args) throws SQLException {
//1.创建数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/text?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("");
//2. 和服务器构造连接
Connection connection = dataSource.getConnection();
//3.构造sql语句
String sql ="select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
//4.执行sql语句
ResultSet resultSet = statement.executeQuery();
//遍历结果
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id= " + id + " ,name= " + name);
}
//5.关闭资源
connection.close();
statement.close();
}