当前位置:Gxlcms > 数据库问题 > JDBC连接MySQL数据库基础

JDBC连接MySQL数据库基础

时间:2021-07-01 10:21:17 帮助过:10人阅读

import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.SQLException; 4 5 public class ExampleDatabase { 6 //定义MySQL的数据库驱动程序 7 public static final String DBDRIVER = "com.mysql.jdbc.Driver"; 8 //定义MySQL数据库的连接地址 9 public static final String DBURL = "jdbc:mysql://localhost:3306/test"; 10 //MySQL数据库的连接用户名 11 public static final String DBUSER = "root"; 12 //MySQL数据库的连接密码 13 public static final String DBPASS = "root"; 14 public static void main(String[] args) { 15 Connection con = null; 16 try { 17 //加载驱动程序 18 Class.forName(DBDRIVER); 19 } 20 catch (ClassNotFoundException e) { 21 e.printStackTrace(); 22 } 23 try { 24 //连接MySQL数据库时,要写上连接的用户名和密码 25 con = DriverManager.getConnection(DBURL, DBUSER, DBPASS); 26 } 27 catch (SQLException e) { 28 e.printStackTrace(); 29 } 30 System.out.println(con); 31 try { 32 //关闭数据库 33 con.close(); 34 } 35 catch (SQLException e) { 36 e.printStackTrace(); 37 } 38 } 39 }

 

执行数据库的更新操作

  数据库连接后,就可以进行数据库的具体操作,要使用Statement接口完成,此接口可以使用Connection接口中提供的createStatement()方法实例化。

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 
 6 public class ExampleDatabase {
 7     //定义MySQL的数据库驱动程序
 8     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
 9     //定义MySQL数据库的连接地址
10     public static final String DBURL = "jdbc:mysql://localhost:3306/test";
11     //MySQL数据库的连接用户名
12     public static final String DBUSER = "root";
13     //MySQL数据库的连接密码
14     public static final String DBPASS = "root";
15     public static void main(String[] args) {
16         Connection con = null;
17         Statement stmt = null;
18         //数据库插入语句
19         String insertSQL = "insert into user (id, name, age) values (3, ‘key‘, 23)";
20         //数据库修改语句
21         String alterSQL = "update user SET name=‘jon‘ where id=8";
22         //数据库删除语句
23         String deleteSQL = "delete from user where id=5";
24         try {
25             //加载驱动程序
26             Class.forName(DBDRIVER);
27         }
28         catch (ClassNotFoundException e) {
29             e.printStackTrace();
30         }
31         try {
32             //连接MySQL数据库时,要写上连接的用户名和密码
33             con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
34             //实例化Statement对象
35             stmt = con.createStatement();
36             //执行数据库更新操作
37             stmt.executeUpdate(insertSQL);
38             stmt.executeUpdate(alterSQL);
39             stmt.executeUpdate(deleteSQL);
40         }
41         catch (SQLException e) {
42             e.printStackTrace();
43         }
44         System.out.println(con);
45         try {
46             //关闭操作
47             stmt.close();
48             //关闭数据库
49             con.close();
50         }
51         catch (SQLException e) {
52             e.printStackTrace();
53         }
54     }
55 }

 

数据库查询操作

  使用SQL的select语句可以查询出数据库的全部结果,在JDBC的操作中数据库的所有查询记录将使用ResultSet进行接收,并使用ResultSet显示内容。要进行数据库查询操作,需要使用Statement接口定义的executeQuery()方法,此方法返回值类型就是一个ResultSet的对象,此对象中存放了所有的查询结果。

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.SQLException;
 4 import java.sql.Statement;
 5 import java.sql.ResultSet;
 6 
 7 public class ExampleDatabase {
 8     //定义MySQL的数据库驱动程序
 9     public static final String DBDRIVER = "com.mysql.jdbc.Driver";
10     //定义MySQL数据库的连接地址
11     public static final String DBURL = "jdbc:mysql://localhost:3306/test";
12     //MySQL数据库的连接用户名
13     public static final String DBUSER = "root";
14     //MySQL数据库的连接密码
15     public static final String DBPASS = "root";
16     public static void main(String[] args) {
17         Connection con = null;
18         Statement stmt = null;
19         ResultSet rs = null;
20         //数据库查询语句
21         String sql = "select id , name, age from user";
22         try {
23             //加载驱动程序
24             Class.forName(DBDRIVER);
25         }
26         catch (ClassNotFoundException e) {
27             e.printStackTrace();
28         }
29         try {
30             //连接MySQL数据库时,要写上连接的用户名和密码
31             con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
32             //实例化Statement对象
33             stmt = con.createStatement();
34             //执行数据库查询操作
35             rs = stmt.executeQuery(sql);
36             while (rs.next()) {
37                 int id = rs.getInt("id");
38                 String name = rs.getString(2);
39                 int age = rs.getInt("age");
40                 System.out.print("id:" + id + " ");
41                 System.out.print("name:" + name + " ");
42                 System.out.println("age:" + age);
43             }
44         }
45         catch (SQLException e) {
46             e.printStackTrace();
47         }
48         System.out.println(con);
49         try {
50             //关闭结果集
51             rs.close();
52             //关闭操作
53             stmt.close();
54             //关闭数据库
55             con.close();
56         }
57         catch (SQLException e) {
58             e.printStackTrace();
59         }
60     }
61 }

  ResultSet中的所有数据都可以通过getString()方法获得。

 

PreparedStatement

  PreparedStatement是Statement的子接口,属于预处理操作。与直接使用Statement不同的是,PreparedStatement在操作时,是先在数据表中准备好了一条SQL语句,但是此SQL语句的具体内容暂时不设置,而是之后再进行设置。由于PreparedStatement对象已预编译过,所以其执行速度要高于Statement对象。在开发中不建议使用Statement,而是使用PreparedStatement。

JDBC连接MySQL数据库基础

标签:数据库基础   变量   oid   操作   ext   ase   简介   tin   默认   

人气教程排行