目录
前言
1.新建项目
2.添加jar包
3.jdbc的连接
package com.wang.dao;
import java.sql.*;
//用Java实现MySQL的增删改查操作
public class Test1Demo {
public static void main(String[]args){
String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";//mysql8的连接字符串,多了时区比之前的5
String name="root";
String password="root";
String sql="SELECT *from tbl_commoditytype";
//1.加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");//在有错误提示的时候光标移到错误处,alt+enter,
try {
//2.创建连接
Connection connection= DriverManager.getConnection(url,name,password);
//3.创建命令窗口
Statement statement = connection.createStatement();
//4.执行命令窗口里的语句
ResultSet resultSet = statement.executeQuery(sql);
//5.处理返回的结果集
while (resultSet.next()){
//打印行的每一列
System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2));
}
//6.关闭资源
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
4、简单的MySQL增删改查操作
package com.wang.demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test01 {
public static void main(String[] args) {//psvm回车可以直接敲出来哦
//1.加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.获取链接,驱动管理器
String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
String user="root";
String password="root";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
//3.获取命令
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
// String sql="insert into tbl_commoditytype (id,name) values (6,'AA')"; //这个地方我后来发现我的数据库表中id忘记设置自增长,就直接在这里直接写上了id的值。之后增删改操作依次执行可查看表得三个结果图
//String sql="update tbl_commoditytype set name ='bb' where id=6";
String sql="delete from tbl_commoditytype where id=6";
int i = 0;
//executeUpdate是做增删改的
//4.得到结果集并处理
try {
i = statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(i);//sout回车可快速创建System.out.println()哦
//5.关闭资源
//用到的connection(连接),statement(命令窗口),两个接口,resultSet一个实现类(结果集)
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.wang.demo;
import java.sql.*;
/**
* 斜杠双星回车即可得这种注释
* 使用Statement进行查询操作
*/
public class Test02 {
public static void main(String[] args) {
//1.加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.获取链接,驱动管理器
String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
String user="root";
String password="root";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
//3.获取命令
Statement statement = null;
try {
statement = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
// String sql="insert into tbl_commoditytype (id,name) values (6,'AA')";
//String sql="update tbl_commoditytype set name ='bb' where id=6";
//String sql="delete from tbl_commoditytype where id=6";
String sql="select *from tbl_commoditytype";
ResultSet resultSet=null;
//executeUpdate是做增删改的
// 4.得到结果集并处理
try {
resultSet = statement.executeQuery(sql);
//处理结果集,两种方法,if(一条记录)/while(不确定或者多条数据)
while(resultSet.next()){
String o= resultSet.getInt(1)+"\t"+resultSet.getString(2);
//因为我的表第一列是int,第二列是string。也可以把2换成name,也就是把索引(columnindex)换成列名(columnlabel}
System.out.println(o);
}
} catch (SQLException e) {
e.printStackTrace();
}
//sout回车可快速创建System.out.println()哦
//5.关闭资源
//用到的connection(连接),statement(命令窗口),两个接口,resultSet一个实现类(结果集)
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}