JDCBC:(Java Databases Connection)java数据库连接。
首先从两方面思考:Java是面向对象语言,SQL面向结构化查询语言,要有MySQL和Java基础,按照我自己的理解:
mysql中的每一张表格对应的就是Java中自定义的类,类中的每一个属性 对应的表中的每一列的列名。
JDBC连接步骤:
1.引入j相应的mysql的jar包:这里引入的是8.0版本的mysql的jar包
2、加载驱动:Class.forName("com.mysql.cj.jdbc.Driver"); 每个版本的mysql加载驱动是不一样的
3、获取连接对象:Connection connection = DriverManager.getConnection(url,user,password);
4、获取执行sql语句的对象:
[Statement statement = connection.createStatement();这个会有sql注入的错误,注:尽量 不用] 下面代码用PreparedStatement这个类以及占位符【?】
5、执行sql 语句:
int row=statement.executeUpdate(sql) 或着
ResultSet resultSet=statement.executeQuery(sql)
6、关闭资源
使用close();
结构:
导入的包: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List;
在dao层中定义一个工具类UserDao,实现对数据库连接的增删改查:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
public Connection connection;
public PreparedStatement ps;
public ResultSet resultSet;
public List<User> findAll(){
List<User> list=new ArrayList<>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?serverTimezone=Asia/Shanghai", "root", "123456");
String sql = "select * from tb_user";
ps = connection.prepareStatement(sql);
resultSet = ps.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setUid(resultSet.getInt("uid"));
user.setUname(resultSet.getString("uname"));
user.setPassword(resultSet.getString("password"));
user.setSex(resultSet.getString("sex"));
list.add(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
return list;
}
public User findById(Integer id){
User user=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?serverTimezone=Asia/Shanghai", "root", "root");
String sql = "select * from tb_user where uid=?";
ps = connection.prepareStatement(sql);
ps.setObject(1, id);
resultSet = ps.executeQuery();
while (resultSet.next()) {
user = new User();
user.setUid(resultSet.getInt("uid"));
user.setUname(resultSet.getString("uname"));
user.setPassword(resultSet.getString("password"));
user.setSex(resultSet.getString("sex"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
return user;
}
public int deleteById(int id){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?serverTimezone=Asia/Shanghai", "root", "root");
String sql = "delete from tb_user where uid=?";
ps = connection.prepareStatement(sql);
ps.setObject(1, id);
int i = ps.executeUpdate();
return i;
}catch (Exception e){
e.printStackTrace();
}finally {
}
return 0;
}
public int update(User user){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?serverTimezone=Asia/Shanghai", "root", "root");
String sql = "update tb_user set uname=?,sex=?,password=? where uid=?";
ps = connection.prepareStatement(sql);
ps.setObject(1, user.getUname());
ps.setObject(2, user.getSex());
ps.setObject(3, user.getPassword());
ps.setObject(4, user.getUid());
int i = ps.executeUpdate();
return i;
}catch (Exception e){
e.printStackTrace();
}finally {
}
return 0;
}
public int insert(User user){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/world?serverTimezone=Asia/Shanghai", "root", "root");
String sql = "insert into tb_user(uid,uname,password,sex) values(null,?,?,?)";
ps = connection.prepareStatement(sql);
ps.setObject(1, user.getUname());
ps.setObject(2, user.getPassword());
ps.setObject(3, user.getSex());
int i = ps.executeUpdate();
return i;
}catch (Exception e){
e.printStackTrace();
}finally {
}
return 0;
}
}
entity:里面定义的一些自定义的类 user
public class User {
private Integer uid;
private String uname;
private String sex;
private String password;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
测试类
package com.ykq.test;
import com.ykq.dao.UserDao;
import com.ykq.entity.User;
import java.util.List;
public class Test {
public static void main(String[] args) {
UserDao userDao=new UserDao();
User user2=new User();
user2.setPassword("110");
user2.setSex("男");
user2.setUname("银河");
user2.setUid(10);
int row = userDao.update(user2);
System.out.println("影响的行数:"+row);
List<User> list = userDao.findAll();
for(int i=0;i<list.size();i++){
User user = list.get(i);
System.out.println("姓名:" + user.getUname());
System.out.println("密码:" + user.getPassword());
System.out.println("性别:" + user.getSex());
System.out.println("id:" + user.getUid());
System.out.println("===========================================");
}
}
}
|