当前位置:   article > 正文

JAVA maven项目 连接数据库 mysql8.0_mysql-connector-java maven

mysql-connector-java maven

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>
  • 1
  • 2
  • 3
  • 4
  • 5

在这里插入图片描述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();
}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

在这里插入图片描述
代码中的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);
	}
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

在这里插入图片描述
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("--------------------");
	}
	
}




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/289846?site
推荐阅读
相关标签
  

闽ICP备14008679号