Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > MySQL JDBC编程

MySQL详解进行JDBC编程与增删改查方法

作者:亚太地区百大最帅面孔第101名

JDBC是指Java数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库。从根本上来说,JDBC 是一种规范,它提供了一套完整的接口,允许便携式访问到底层数据库

Java的数据库编程JDBC

概念

使用步骤

创建DataSource对象,这个对象就描述了数据库服务器在哪

DataSource dataSource = new MysqlDataSource();
		//设置数据库所在的地址
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/lmp?characterEncoding=utf8&useSSL=false");
        //设置登录数据库的用户名
        ((MysqlDataSource)dataSource).setUser("root");
        //设置登录数据库的密码
        ((MysqlDataSource)dataSource).setPassword("woshizhu123");

通过Connection连接数据库(输入密码连接成功)

//import java.sql.Connection;
 Connection connection  = dataSource.getConnection();

拼接sql语句(写入sql语句)

String sql = "insert into student values(1,'张三')";

将sql语句包装成对象

PreparedStatement statement = connection.prepareStatement(sql);

执行sql语句(按下回车执行sql语句)

int ret = statement.executeUpdate();

释放资源

 statement.close();
 connection.close();

利用JDBC实现增加(insert)

public class TestJDBC {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf-8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("woshizhu123");
        Connection connection = dataSource.getConnection();
        System.out.println("输入id");
        int id = scanner.nextInt();
        System.out.println("输入名字");
        String name = scanner.next();
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        statement.setString(2,name);
        int ret = statement.executeUpdate();
        if(ret == 1){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
        statement.close();
        connection.close();
    }
}

利用JDBC实现删除(delete)

public class TestJDBCDelete
{
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("woshizhu123");
        Connection connection = dataSource.getConnection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        int id = scanner.nextInt();
        String sql = "delete from student where id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        int ret = preparedStatement.executeUpdate();
        System.out.println(ret);
        preparedStatement.close();
        connection.close();
    }

利用JDBC实现修改(update)

public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("woshizhu123");
        Connection connection = dataSource.getConnection();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生id");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的学生姓名");
        String name = scanner.next();
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        int ret = statement.executeUpdate();
        System.out.println(ret);
        statement.close();
        connection.close();
    }
}

利用JDBC实现查找(select)

public static void testJDBCSelect() throws SQLException {
        //1创建DataSource对象
        DataSource dataSource = new MysqlDataSource();
        //2连接数据库
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("listen");
        Connection connection = dataSource.getConnection();
        //3拼接sql
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4执行sql
        ResultSet resultSet = statement.executeQuery();
        //5遍历得到的集合
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int classId = resultSet.getInt("classId");
            System.out.println("id " + id + " name " + name + " classId " + classId);
        }
        //6关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }

到此这篇关于MySQL详解进行JDBC编程与增删改查方法的文章就介绍到这了,更多相关MySQL JDBC编程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文