1 link
2, choose to download content, and download
- OS Select Platform Independent
- Two records in the list below, tar.gz suffix is the Linux version, .zip is the windows version.
- Here to download the windows version of jdbc driver, version 8.0.18
- Skip to login, click on the red box's contents, download
3, the drive import java project
- Create a java project in which IDEA
- Select Create a Java project, JDK 1.8 selected here, directly Next
- Check the press template creation, Next
- Enter a project name and project address, Finish
- Project created the interface is as follows
- In a project to build a file named lib (name recommended to use lib) folder
- Downloaded to the zip file to decompress it (mysql-connector-java-8.0.18.jar), and enter the unzipped folder
- Copy of which mysql-connector-java-8.0.18.jar file, right-lib folder and paste it into the IDEA, just the new lib folder
4, so that the imported drivers into force
- In IDEA, click File - Project Structure
- Which Module module, Dependencies tab
- Click on the far right of the plus sign (+), select JARS or directories
- In the pop-up window, select the drive you just imported lib folder, click Ok
- We can see Module module, mysql more of a drive, and finally click Apply, then Ok
5, registration database driver, database and insert data
- Create a new class in the src LinkDatabseInsert
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.SQLException; 5 6 public class LinkDatabseInsert{ 7 public static void main(String[] args) throws ClassNotFoundException, SQLException { 8 //1.注册数据库的驱动 9 Class.forName("com.mysql.jdbc.Driver"); 10 //2.获取数据库连接(里面内容依次是:"jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码") 11 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 12 //3.需要执行的sql语句(?是占位符,代表一个参数) 13 String sql = "insert into stu(id,name,age) values(?,?,?)"; 14 //4.获取预处理对象,并依次给参数赋值 15 PreparedStatement statement = connection.prepareCall(sql); 16 statement.setInt(1,10); //数据库字段类型是int,就是setInt;1代表第一个参数 17 statement.setString(2,"小明"); //数据库字段类型是String,就是setString;2代表第二个参数 18 statement.setInt(3,16); //数据库字段类型是int,就是setInt;3代表第三个参数 19 //5.执行sql语句(执行了几条记录,就返回几) 20 int i = statement.executeUpdate(); 21 System.out.println(i); 22 //6.关闭jdbc连接 23 statement.close(); 24 connection.close(); 25 } 26 }
- 运行程序,返回1,说明成功向mysql数据库内插入一条数据
6、注册数据库驱动,并更新数据库内记录
- 在src下新建一个LinkDatabseUpdate类
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.SQLException; 5 6 public class LinkDatabaseUpdate { 7 public static void main(String[] args) throws ClassNotFoundException, SQLException { 8 //1.注册数据库的驱动 9 Class.forName("com.mysql.jdbc.Driver"); 10 //2.获取数据库连接(里面内容依次是:"jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码") 11 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 12 //3.需要执行的sql语句(?是占位符,代表一个参数) 13 String sql = "update stu set name=?,age=? where id=?"; 14 //4.获取预处理对象,并依次给参数赋值 15 PreparedStatement statement = connection.prepareCall(sql); 16 statement.setString(1,"小黄"); //数据库字段类型是String,就是setString;1代表第一个参数 17 statement.setInt(2,28); //数据库字段类型是int,就是setInt;2代表第二个参数 18 statement.setInt(3,10); //数据库字段类型是int,就是setInt;3代表第三个参数 19 //5.执行sql语句(执行了几条记录,就返回几) 20 int i = statement.executeUpdate(); 21 System.out.println(i); 22 //6.关闭jdbc连接 23 statement.close(); 24 connection.close(); 25 } 26 }
- 运行程序,返回1,说明更新id为10的记录成功
7、注册数据库驱动,并删除数据库内记录
- 在src下新建一个LinkDatabseDelete类
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.SQLException; 5 6 public class LinkDatabaseDelete { 7 public static void main(String[] args) throws ClassNotFoundException, SQLException { 8 //1.注册数据库的驱动 9 Class.forName("com.mysql.jdbc.Driver"); 10 //2.获取数据库连接(里面内容依次是:"jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码") 11 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 12 //3.需要执行的sql语句(?是占位符,代表一个参数) 13 String sql = "delete from stu where id=?"; 14 //4.获取预处理对象,并依次给参数赋值 15 PreparedStatement statement = connection.prepareCall(sql); 16 statement.setInt(1,9); //字段类型是int,就是setInt;1代表第一个参数 17 //5.执行sql语句(执行了几条记录,就返回几) 18 int i = statement.executeUpdate(); 19 System.out.println(i); 20 //6.关闭jdbc连接 21 statement.close(); 22 connection.close(); 23 } 24 }
- 运行程序,返回1,说明删除id为9的记录成功
8、注册数据库驱动,并查询数据库内记录
- 在src下新建一个LinkDatabseSelect类
1 import java.sql.*; 2 3 public class LinkDatabseSelect { 4 public static void main(String[] args) throws ClassNotFoundException, SQLException { 5 //1.注册数据库的驱动 6 Class.forName("com.mysql.jdbc.Driver"); 7 //2.获取数据库连接(里面内容依次是:"jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码") 8 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 9 //3.需要执行的sql语句(?是占位符,代表一个参数) 10 String sql = "select * from stu where id in(?,?)"; 11 //4.获取预处理对象,并给参数赋值 12 PreparedStatement statement = connection.prepareStatement(sql); 13 statement.setInt(1,1); //字段类型是int,就是setInt;1代表第一个参数 14 statement.setInt(2,4); //字段类型是int,就是setInt;2代表第一个参数 15 //5.执行sql语句(返回结果集) 16 ResultSet resultSet = statement.executeQuery(); 17 while(resultSet.next()){ //如果有下一条记录 18 String id = resultSet.getString("id"); //根据列名返回值 19 String name = resultSet.getString("name"); //根据列名返回值 20 int age = resultSet.getInt(3); //根据列的顺序返回值 21 String gender = resultSet.getString(4); //根据列的顺序返回值 22 System.out.println(id+"\t"+name+"\t"+age+"\t"+gender); 23 } 24 //6.关闭jdbc连接 25 resultSet.close(); 26 statement.close(); 27 connection.close(); 28 } 29 }
- 运行程序,返回了两条记录,说明查询记录成功