1.mysql数据库驱动的下载以及配置
-
1.在百度中搜索mysql connector maven(以后下载java相关的jar包后边+maven)
选择你要下载的版本
1.png -
2.选择file-project structure-Moudules,将刚才下载好的jar包添加即可
2.png
3.png
-
3.点击右边框的database,测试mysql数据库的连接状态
4.png
5.png
6.png
2.编写java代码用sql语句向数据库中写入数据
- 1.写入数据
package JDBC;
//导入sql相关的包
import java.sql.*;
public class JdbcDemo {
//URL:其中user表示向mysql中名为user的数据库写入数据(填入你的数据库名)
static String URL = "jdbc:mysql://localhost:3306/数据库名称";
//数据库的用户名
static String USERNAME = "用户名";
//数据库的密码
static String PWD = "密码";
public static void update() {// 增删改
Connection connection = null;
Statement stmt = null;
try {
// 1.导入驱动,加载具体的驱动类
//这里需要注意mysql版本的问题老版本的mysql驱动名为com.mysql.jdbc.Driver
//新版本的为com.mysql.cj.jdbc.Driver,具体的情况视自己的mysql版本而定
Class.forName("com.mysql.cj.jdbc.Driver");// 加载具体的驱动类
// 2.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// 3.发送sql,执行(增删改、查)
stmt = connection.createStatement();
String sql = "insert into users values('zs','s1')";
//String sql = "update student set STUNAME='ls' where stuno=1";
// String sql = "delete from student where stuno=1";
// 4.执行SQL
//增删改executeUpdate(sql)
//查询executeQuery(sql)
int count = stmt.executeUpdate(sql); // 返回值表示 增删改 几条数据
// 5.处理结果
if (count > 0) {
System.out.println("操作成功!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(stmt!=null) stmt.close();// 对象.方法
if(connection!=null)connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JdbcDemo jdbc =new JdbcDemo();
jdbc.update();
}
}
- 2.查询数据(新建ResultSet结果集用于接受executeQuery方法返回的信息)
package JDBC;
//导入sql相关的包
import java.sql.*;
public class JdbcDemo {
//URL:其中user表示向mysql中名为user的数据库写入数据(填入你的数据库名)
static String URL = "jdbc:mysql://localhost:3306/user";
//数据库的用户名
static String USERNAME = "root";
//数据库的密码
static String PWD = "你的密码";
public static void update() {// 增删改
Connection connection = null;
Statement stmt = null;
ResultSet rs=null; //查询的结果集 类似于一张表 rs指针默认指向结果集的前一行
//rs.next(); 1.下移 2.判断下移之后是否有数据 rs.getint() rs.getString() rs.getDate 获取数据
try {
// 1.导入驱动,加载具体的驱动类
Class.forName("com.mysql.cj.jdbc.Driver");// 加载具体的驱动类
// 2.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// 3.发送sql,执行(增删改、查)
stmt = connection.createStatement();
// String sql = "insert into users values('马骥','123')";
//String sql = "update student set STUNAME='ls' where stuno=1";
// String sql = "delete from student where stuno=1";
String sql ="select name ,password from users";
// 4.执行SQL
//查询用executeQuery,增删改用executeUpdate
rs = stmt.executeQuery(sql); // 返回值表示 增删改 几条数据
// 5.处理结果
/*if (count > 0) {
System.out.println("操作成功!");
}*/
while(rs.next()){
String sname=rs.getString("name");
String spwd=rs.getString("password");
System.out.println(sname+"--"+spwd);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();// 对象.方法
if(connection!=null)connection.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
JdbcDemo jdbc =new JdbcDemo();
jdbc.update();
}
}
3.查询数据库写入情况
7.png
***注:之前使用bash现在更换为zsh,mysql配置路径需要在
- 1.执行下面的语句进行更改
vi ~/.zshrc
- 2.使配置文件生效
source ~/.zshrc
- 3.进入 mysql
mysql -u root -p;