JDBC常见方法

使用java程序访问MySQL数据库时,我们需要借助JDBC来访问数据库
常见的MYSQL数据库操作命令主要有创建表和操作表(增删改查)
使用数据库
use (数据库名);
创建数据库:
create database [数据库名称] ;
创建表
create table {表名}{
//
}
删除表
DROP TABLE table_name ;
删除数据
DELETE FROM <表名> [WHERE 子句] [ORDER BY 子句] [LIMIT 子句]
修改数据
UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ][ORDER BY 子句] [LIMIT 子句]
查询数据
select (。。。)from (表名);
查询数据库所有数据
select * from (数据库名);
使用JDBC连接数据库的方法是:
1、加载驱动

	Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动

2、连接数据库

	connection = DriverManager.getConnection("jdbc:mysql://localhost/db1", "root", "root");

3、设置SQL语句

	String sql = "insert into student values(5,'kate','123456')";

4、创建statement对象

	statement = connection.createStatement();

5、执行SQL语句并进行处理

  	int i = statement.executeUpdate(sql);//执行SQL
    System.out.println(i);//影响行数

最后要对其进行释放:

		finally {
//            statement.close();
            //7.释放资源
            //避免空指针异常
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

jdbc的前期操作几乎完全一样,为了简化代码,我们可以将前面的代码块进行整合,然后输入SQL语句进行执行

	private static void login(String sql) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
            //获取数据库的连接对象
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/user_info", "root", "root");
            //定义SQL语句
            //5.获取SQL执行的对象Statement
            Statement statement = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

增添数据演示

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

// 向表中添加一条数据
public class JDBC01 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
            String sql = "insert into student values(5,'kate','123456')";
            //2.连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://localhost/db1", "root", "root");
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);//执行SQL
            System.out.println(i);//影响行数
            if(i>0){
                System.out.println("添加成功");
            }else System.out.println("添加失败");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
//            statement.close();
            //7.释放资源
            //避免空指针异常
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

修改表中数据

//修改表中数据
public class JDBC02 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
            String sql = "update student set password = '987654' where id = 5";
            //2.连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://localhost/db1", "root", "root");
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);//执行SQL
            System.out.println(i);//影响行数
            if(i>0){
                System.out.println("修改成功");
            }else System.out.println("修改失败");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
//            statement.close();
            //7.释放资源
            //避免空指针异常
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

删除表中的一个数据

//删除表中的一条内容
public class JDBC03 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
            String sql = "delete from student where name = 'cell'";
            //2.连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://rm-2zeojvo8ce2o612459o.mysql.rds.aliyuncs.com/user_info", "lin", "Ylg19981127");
            statement = connection.createStatement();
            int i = statement.executeUpdate(sql);//执行SQL
            System.out.println(i);//影响行数
            if(i>0){
                System.out.println("删除成功");
            }else System.out.println("删除失败");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
//            statement.close();
            //7.释放资源
            //避免空指针异常
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

查询表中数据

public class JDBC05 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//注册驱动
            String sql = "select * from student";
            //2.连接数据库
            connection = DriverManager.getConnection("jdbc:mysql://rm-2zeojvo8ce2o61245125010.mysql.rds.aliyuncs.com:3306/user_info", "lin", "Ylg19981127");
            statement = connection.createStatement();
            //执行SQL语句
            resultSet = statement.executeQuery(sql);
            //处理结果
            resultSet.next();//让光标向下移动一行
            //获取结果
            System.out.println(resultSet.getInt(1)+resultSet.getString("name"));
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
//            statement.close();
            //7.释放资源
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            //避免空指针异常
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(JDBC常见方法)