Java连Mysql接数据库(1)

连接步骤:

1.导入驱动jar包 mysql-connector-java-8.0.28.jar

  ①在项目目录下创建libs文件夹

  ②复制jar包到项目的libs目录下

  ③右键-->Add As Library

2.注册驱动:告诉程序该使用哪个数据库驱动jar包(mysql5之后的驱动jar包可以省略这个步骤)

3.获取数据库连接对象(Connection)

  参数:

    url:指定连接的路径

      语法:jdbc:mysql://ip地址(域名):端口号/数据库名称

      例子:jdbc:mysql://localhost:3306/student

      访问本机数据库可简写:jdbc:mysql:///student

    user:用户名

    password:密码

4.定义sql

5.获取执行sql语句对象(Statement)

  boolean execute(String sql):可以执行任意sql语句

  int executeUpdate(String sql):执行DML(insert、update、delete)语句、DDL(create、alter、drop)语句

  ResultSet executeQuery(String sql):执行DQL(select)语句

6.执行sql,接受返回结果

7.处理结果

8.释放资源

代码实现1:

复制代码
 1 package com.chen.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.Statement;
 6 
 7 /**
 8  * jdbc快速入门     修改数据
 9  */
10 public class JdbcDemo1 {
11     public static void main(String[] args) throws Exception {
12         //1.导入驱动jar包
13         //2.注册驱动
14         Class.forName("com.mysql.jdbc.Driver");
15 
16         //3.获取数据库连接对象
17         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
18 
19         //4.定义sql语句
20         String sql="update login set username = \"zqchen\" where id = 1";
21         System.out.println(sql);
22 
23         //5.获取执行sql的对象 Statement
24         Statement stmt=conn.createStatement();
25 
26         //6.执行sql
27         int count = stmt.executeUpdate(sql);
28         System.out.println(count);
29 
30         //7.释放资源
31         stmt.close();
32         conn.close();
33     }
34 }
更新数据
复制代码

代码实现2:

复制代码
 1 package com.chen.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 
 8 /**
 9  * 插入数据
10  */
11 public class JdbcDemo2 {
12     public static void main(String[] args){
13         Connection conn = null;
14         Statement stmt = null;
15         try {
16             //注册驱动
17             Class.forName("com.mysql.jdbc.Driver");
18             //获取Connection对象
19             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
20             //获取执行sql的对象Statement
21             stmt = conn.createStatement();
22             //定义sql语句
23             String sql="insert into login values(null,'王五','abcdef')";
24             //执行sql语句
25             int count = stmt.executeUpdate(sql);//影响的行数
26             System.out.println(count);
27             if(count>0){
28                 System.out.println("添加成功!");
29             }else{
30                 System.out.println("添加失败!");
31             }
32         } catch (ClassNotFoundException e) {
33             e.printStackTrace();
34         } catch (SQLException e) {
35             e.printStackTrace();
36         }finally{
37             //释放资源
38             //判断,避免空指针异常
39             if(stmt!=null){
40                 try {
41                     stmt.close();
42                 } catch (SQLException e) {
43                     e.printStackTrace();
44                 }
45             }
46             if(conn!=null){
47                 try {
48                     conn.close();
49                 } catch (SQLException e) {
50                     e.printStackTrace();
51                 }
52             }
53         }
54     }
55 }
添加数据
复制代码

代码实现2:

复制代码
 1 package com.chen.jdbc;
 2 
 3 import java.sql.*;
 4 
 5 /**
 6  * 查询数据     stmt.executeQuery(sql);
 7  */
 8 public class JdbcDemo3 {
 9     public static void main(String[] args){
10         Connection conn = null;
11         Statement stmt = null;
12         ResultSet rs = null;
13         try {
14             //注册驱动
15             Class.forName("com.mysql.jdbc.Driver");
16             //获取Connection对象
17             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
18             //获取执行sql的对象Statement
19             stmt = conn.createStatement();
20             //定义sql语句
21             String sql="select * from login";
22             //执行sql语句
23             rs = stmt.executeQuery(sql);
24             while(rs.next()){
25                 int id=rs.getInt(1);
26                 String username=rs.getString("username");
27                 String password=rs.getString(3);
28                 System.out.println(id+"---"+username+"---"+password);
29             }
30         } catch (ClassNotFoundException e) {
31             e.printStackTrace();
32         } catch (SQLException e) {
33             e.printStackTrace();
34         }finally{
35             //释放资源
36             //判断,避免空指针异常
37             if(rs!=null){
38                 try {
39                     rs.close();
40                 } catch (SQLException e) {
41                     e.printStackTrace();
42                 }
43             }
44             if(stmt!=null){
45                 try {
46                     stmt.close();
47                 } catch (SQLException e) {
48                     e.printStackTrace();
49                 }
50             }
51             if(conn!=null){
52                 try {
53                     conn.close();
54                 } catch (SQLException e) {
55                     e.printStackTrace();
56                 }
57             }
58         }
59     }
60 }
查询语句
复制代码

工具类实现:

复制代码
  1 package com.chen.util;
  2 
  3 import java.io.FileReader;
  4 import java.io.IOException;
  5 import java.net.URL;
  6 import java.sql.*;
  7 import java.util.Properties;
  8 
  9 /**
 10  * jdbc工具类
 11  */
 12 public class JDBCUtils {
 13     private static String url;
 14     private static String user;
 15     private static String password;
 16     private static String driver;
 17 
 18     /**
 19      * 文件的读取,只需要读取一次即可。使用静态代码块
 20      */
 21     static{
 22         //读取资源文件,获取值
 23         try {
 24             //获取src路径下的文件方式--->ClassLoader 类加载器
 25             ClassLoader classLoader=JDBCUtils.class.getClassLoader();
 26             URL res = classLoader.getResource("jdbc.properties");
 27             //把URL类型转换为String类型
 28             String path= res.getPath();
 29             System.out.println(path);
 30 
 31             //创建properties集合类
 32             Properties pro=new Properties();
 33             //加载文件
 34             pro.load(new FileReader(path));
 35             //获取文件,赋值
 36             url=pro.getProperty("url");
 37             user=pro.getProperty("user");
 38             password=pro.getProperty("password");
 39             driver=pro.getProperty("driver");
 40 
 41             //注册驱动
 42             Class.forName(driver);
 43         } catch (IOException e) {
 44             e.printStackTrace();
 45         } catch (ClassNotFoundException e) {
 46             e.printStackTrace();
 47         }
 48     }
 49 
 50     /**
 51      * 获取连接
 52      * @return  连接对象
 53      */
 54     public static Connection getConnecttion() throws SQLException {
 55         return DriverManager.getConnection(url,user,password);
 56     }
 57 
 58     /**
 59      * 释放资源
 60      * @param stmt
 61      * @param conn
 62      */
 63     public static void close(Statement stmt, Connection conn){
 64         if(stmt!=null){
 65             try {
 66                 stmt.close();
 67             } catch (SQLException e) {
 68                 e.printStackTrace();
 69             }
 70         }
 71         if(conn!=null){
 72             try {
 73                 conn.close();
 74             } catch (SQLException e) {
 75                 e.printStackTrace();
 76             }
 77         }
 78     }
 79 
 80     /**
 81      * close方法重载
 82      * @param rs
 83      * @param stmt
 84      * @param conn
 85      */
 86     public static void close(ResultSet rs, Statement stmt, Connection conn){
 87         if(rs!=null){
 88             try {
 89                 rs.close();
 90             } catch (SQLException e) {
 91                 e.printStackTrace();
 92             }
 93         }
 94         if(stmt!=null){
 95             try {
 96                 stmt.close();
 97             } catch (SQLException e) {
 98                 e.printStackTrace();
 99             }
100         }
101         if(conn!=null){
102             try {
103                 conn.close();
104             } catch (SQLException e) {
105                 e.printStackTrace();
106             }
107         }
108     }
109 }
JDBC工具类
复制代码
复制代码
 1 package com.chen.jdbc;
 2 
 3 import com.chen.util.JDBCUtils;
 4 
 5 import java.sql.*;
 6 
 7 /**
 8  * 工具类实现查询数据
 9  */
10 public class JdbcDemo4 {
11     public static void main(String[] args){
12         Connection conn = null;
13         Statement stmt = null;
14         ResultSet rs = null;
15         try {
16             //获取Connection对象
17             conn = JDBCUtils.getConnecttion();
18             //获取执行sql的对象Statement
19             stmt = conn.createStatement();
20             //定义sql语句
21             String sql="select * from login";
22             //执行sql语句
23             rs = stmt.executeQuery(sql);
24             while(rs.next()){
25                 int id=rs.getInt(1);
26                 String username=rs.getString("username");
27                 String password=rs.getString(3);
28                 System.out.println(id+"---"+username+"---"+password);
29             }
30         } catch (SQLException e) {
31             e.printStackTrace();
32         }finally{
33             //释放资源
34             JDBCUtils.close(rs,stmt,conn);
35         }
36     }
37 }
使用工具类实现
复制代码
复制代码
1 url=jdbc:mysql://localhost:3306/student
2 user=root
3 password=123456
4 driver=com.mysql.jdbc.Driver
jdbc.properties
复制代码

 jdbc.properties文件放在项目src目录下。

posted @   1281024  阅读(88)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示