JAVA maven项目 连接数据库 mysql8.0

本文介绍如何在Java Maven项目中使用MySQL 8.0进行数据库操作,包括连接数据库、执行查询、插入、删除和更新操作。文中详细展示了如何在pom.xml文件中添加mysql-connector-java依赖,并提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA maven项目 连接数据库 mysql8.0
查询,插入,删除,更新

1.在pom.xml加入mysql-connector-java依赖包,version要跟你的mysql版本对应

 <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.17</version>
</dependency>

在这里插入图片描述2.创建java ,MySQLDemo.java
先连接数据库,要开启mysql

package demo;

import java.sql.*;

public class MySQLDemo {
 
    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
    //static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    //static final String DB_URL = "jdbc:mysql://localhost:3306/runoob?characterEncoding=utf8&useSSL=true";
 
    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    Connection conn = null;
    Statement stmt = null;
    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
 
 
    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "123456";
    public MySQLDemo() {
    try {
				Class.forName(JDBC_DRIVER);
				System.out.println("加载数据库驱动成功");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	try {
		conn = DriverManager.getConnection(DB_URL, USER, PASS);
		System.out.println("连接数据库驱动成功");
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
    public static void main(String[] args) {
        MySQLDemo demo=new MySQLDemo();
}}

在这里插入图片描述
代码中的String DB_URL = “jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC”;
加粗的地方对应mysql这里,如图。还有用户名跟密码要改,其他不变。还有版本号不一也要改,代码上有写
在这里插入图片描述
另外一种

package cn.itcast.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

public class Conn {
	private Connection conn ; 

	public Conn()
	{
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
			 conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.100.101:3306/users", "root", "123456");
			 System.out.println("连接成功");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
	}
	@Test
	public void test() {
		System.out.println(conn);
	}
}


在这里插入图片描述
3.加上查询,插入,删除,更新的完整代码,在基本上改就行

package demo;

import java.sql.*;

public class MySQLDemo {
 
    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
    //static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    //static final String DB_URL = "jdbc:mysql://localhost:3306/runoob?characterEncoding=utf8&useSSL=true";
 
    // MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    Connection conn = null;
    Statement stmt = null;
    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
 
 
    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "ZJM199933";
    public MySQLDemo() {
     try {
				Class.forName(JDBC_DRIVER);
				System.out.println("加载数据库驱动成功");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	try {
		conn = DriverManager.getConnection(DB_URL, USER, PASS);
		System.out.println("连接数据库驱动成功");
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
public void query() {
    	try{
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);
        
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    	System.out.println("");
    }
    public void insert() {
    	
    	String sql1 = "insert into websites(id,name,url,alexa,country) values(6,'java','www.baidu',6,'hmoe')";
        PreparedStatement ps = null;
		
		try {
			ps = conn.prepareStatement(sql1);
			ps.executeUpdate();
			System.out.println("插入成功!");

			System.out.println("插入结束!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	//查询
	public void like(){
		
		String sql = "select * from hot where name like '%zhang%'";
		PreparedStatement ps = null;
		ResultSet res = null;
			try {
				ps = (PreparedStatement) conn.prepareStatement(sql);
				res = ps.executeQuery();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {

				while(res.next()){
					
					int num = res.getInt(1);
					String name = res.getString(2);
					String author = res.getString(3);
					String style = res.getString(4);
					String form = res.getString(5);
					
					
					System.out.println("num: " + num + " ,name: " + name +" ,author: " + author + " ,style: " + style + " ,form: " + form);
					System.out.println("模糊查询成功");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		} 

	
	//更新
	public int update(){
		String sql = "update hot set name = '张军' where num=7";
		PreparedStatement ps = null;
		
		try {
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.executeUpdate();
			System.out.println("更新成功!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;

	}
		
	//删除
	public void delete(){
		
		String sql = "delete from hot where num = 1";
		String sql2 = "delete from hot where num = 2";
		String sql3 = "delete from hot where num = 3";
		String sql4 = "delete from hot where num = 4";
		String sql5 = "delete from hot where num = 5";
		
		PreparedStatement ps = null;
		
		try {
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.executeUpdate();
			System.out.println("删除成功!");
			ps = (PreparedStatement) conn.prepareStatement(sql2);
			ps.executeUpdate();
			System.out.println("删除成功!");
			ps = (PreparedStatement) conn.prepareStatement(sql3);
			ps.executeUpdate();
			System.out.println("删除成功!");
			ps = (PreparedStatement) conn.prepareStatement(sql4);
			ps.executeUpdate();
			System.out.println("删除成功!");
			ps = (PreparedStatement) conn.prepareStatement(sql5);
			ps.executeUpdate();
			System.out.println("删除成功!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public static void main(String[] args){
		
		 MySQLDemo conn2 = new  MySQLDemo();
		conn2.insert();
		System.out.println("--------------------");
		conn2.like();
		System.out.println("--------------------");
		conn2.update();
		System.out.println("--------------------");
//		conn2.delete();
		System.out.println("--------------------");
		conn2.query();
		System.out.println("--------------------");
	}
	
}




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值