JDBC入门

本文介绍了如何使用Java模拟JDBC接口来操作MySQL数据库,包括建立连接、执行CRUD操作和关闭连接。同时展示了在IDEA中直接操作数据库的示例,并详细列举了5种不同的数据库连接方式,从静态加载驱动到使用配置文件加载,涵盖了各种场景的应用。
摘要由CSDN通过智能技术生成

 

java程序模拟JDBC

建立一个接口代表jdbc

package JDBC;

/**
 * @author whlie(true){learn}
 * 规定的JDBC连接方法
 */
public interface Interface {
    //连接
    public void Connect();
    //操作
    public void crud();
    //关闭
    public void close();
}

编写Mysql类代表mysql

package JDBC;

/**
 * @author whlie(true){learn}
 * 模拟实现JDBC接口
 */
public class Mysql implements Interface{

    @Override
    public void Connect() {
        System.out.println("连接成功");
    }

    @Override
    public void crud() {
        System.out.println("操作成功");
    }

    @Override
    public void close() {
        System.out.println("关闭连接");
    }
}

编写test代表idea

package JDBC;

/**
 * @author whlie(true){learn}
 */
public class Test {
    public static void main(String[] args) {
        Interface in=new Mysql();
        in.Connect();
        in.crud();
        in.close();
    }
}

实际的操作,在sql中创建表

CREATE TABLE actor(
	id INT PRIMARY KEY AUTO_INCREMENT,
	`name` VARCHAR(32) NOT NULL DEFAULT '',
	sex VARCHAR(32) NOT NULL DEFAULT '女',
	borndate DATETIME,
	phone VARCHAR(12))
SELECT *FROM actor

在idea中对该表进行操作

package JDBC;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author whlie(true){learn}
 */
public class Jdbc01 {
    public static void main(String[] args) throws SQLException {
        //1.注册驱动
        Driver driver = new Driver();
        /**
         * 2.建立连接
         * jdbc:mysql规定好表示协议,通过jdbc的方式连接mysql
         * localhost表示主机,也可以用IP地址
         * 3306表示mysql监听的端口
         * wzg_db02连接数据库的名称
         */
        String url="jdbc:mysql://localhost:3306/wzg_db02";
        //将用户名和密码放入Properties对象
        Properties properties = new Properties();
        //user和password是规定好的,后面的值根据情况写
        properties.setProperty("user","root");
        properties.setProperty("password","***");
        Connection connect = driver.connect(url, properties);
        //3.执行sql语句
        String sql="insert into actor values(01,'周星驰','男','1999-09-09','001')";
        //statement用于执行静态sql语句
        Statement statement = connect.createStatement();
        //i表示影响的行数,如果>0说明成功
        int i = statement.executeUpdate(sql);
        System.out.println(i>0?"成功":"失败");
        //4.关闭连接
        statement.close();
        connect.close();
    }
}

连接数据库的5种方式

方式1静态加载

        Driver driver = new Driver();
        String url="连接地址";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","***");
        Connection connect = driver.connect(url, properties);
        String sql="sql语句";
        Statement statement = connect.createStatement();
        int i = statement.executeUpdate(sql);
        statement.close();
        connect.close();

方式2动态加载,更加灵活减少依赖性

        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();

方式3使用DriverManager

        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver)aClass.newInstance();
        String url="连接地址";
        String user="root";
        String password="***";
        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

方式4 Class.forName自动完成注册驱动

        //其实这一句也可以不写,因为jdk1.5后有自动注册功能
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/wzg_db02";
        String user="root";
        String password="***";
        Connection connection = DriverManager.getConnection(url, user, password);

方式5 配置文件+方式4

配置文件

url=jdbc:mysql://localhost:3306/wzg_db02;
user=root;
password=***;
driver=com.mysql.jdbc.Driver
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql"));
        String driver= properties.getProperty("driver");
        String url=properties.getProperty("url");
        String user=properties.getProperty("user");
        String password=properties.getProperty("password");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值