-- 创建user表
CREATE TABLE user(
uid int PRIMARY KEY auto_increment,
uname VARCHAR(55),
pwd VARCHAR(55),
realName VARCHAR(55)
)
SELECT * FROM user;
Jdbc增删改查
package myjdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class myJDBC {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.类加载,加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.链接数据库,获取链接对象
// Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb01", "root","123456");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false&"+"useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai", "root","123456");
//3.创建命令发送器(手枪)
Statement statement=conn.createStatement();
//4.执行sql语句,返回结果(相当于子弹)。executeUpdate:增加,删除,修改
// int update=statement.executeUpdate("insert into user values(default,'ww','234','王小五a')");
//删除操作
// int update=statement.executeUpdate("delete from user where uid=6");
//修改操作
// int update=statement.executeUpdate("update user set uname='想',realname='你' where uid=7");
//单取一条数据
ResultSet re=statement.executeQuery("select * from user where uid=7");
// ResultSet re=statement.executeQuery("select * from user");
while(re.next()){
int id = re.getInt(1);
String name = re.getString(2);
String pwd = re.getString(3);
String rname=re.getString(4);
System.out.println(id+name+pwd+rname);
}
//5.处理结果
// if (update>0) {
// System.out.println("操作成功");
// } else {
// System.out.println("操作失败!");
// }
//6.关闭资源
statement.close();
conn.close();
}
}