JAVA使用JDBC的六步操作以及进行insert和select-----JDBC

import org.junit.jupiter.api.Test;

import java.sql.*;

public class JDBC
{
    @Test
    public void TestProperties()
    {
        Statement statement = null;
        ResultSet resultSet = null;
        Connection connection = null;
        try
        {
            //JDBC需要配置系统环境变量中,classpath设置.:加上jar包名字以及所在的完整路径,IDEA可以后台完成这部分设置
            //JDBC编程6步
            //注册驱动Driver,获取链接Connection,获取操作对象Statement,执行SQL语句,处理查询结果集,释放资源
            //注册驱动,相当于通知Java程序我们要连接哪个数据库,将Mysql(厂商写的Driver实现类传到DriverManager下)
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //获取链接,相当于建立JVM和数据库这两个进程之间的通道,这是重量级的进程之间的通信,结束一定要关闭掉
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:13306/mall","root","abc123");
            //URL统一资源定位符,网络中某个资源的绝对路径,网址就是URL,包括了协议(HTTP://),IP(182.61.200.7),PORT(80),资源名(index.html)
            //同理可得,URL为jdbc:mysql://为协议,localhost为IP地址,端口号为13306,资源为mall
            //什么是通信协议?有什么用,通信协议是通信之前提前定好的数据传输协议,数据包具体怎么传输,要遵循提前定好的格式
            statement = connection.createStatement();
            String sql = "insert into users(username,pssword,email) values('Susan','123456','123@qq.com')";
            //专门执行DML语句的,返回影响数据库中的记录条数
            int i = statement.executeUpdate(sql);
            resultSet = statement.executeQuery("select * from users");
            System.out.println(i == 1 ? "保存成功" : "保存失败");
            while (resultSet.next())
            {
                String username = resultSet.getString("username");
                String password = resultSet.getString("pssword");
                String email = resultSet.getString("email");
                System.out.println(username);
                System.out.println(password);
                System.out.println(email);
            }
            //获取数据库操作对象,专门用于执行SQL语句的对象
            //执行SQL语句
            //处理查询结果集,只有第四步是Select语句才有处理结果集
            //释放资源(两者之间属于进程间通信),防止内存泄漏
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //释放资源一定要分别对其进行try..catch操作,避免无法关闭成功
            try
            {
                if(statement != null)
                {
                    statement.close();
                }
            }
            catch (SQLException e)
            {
                throw new RuntimeException(e);
            }
            try
            {
                if (resultSet != null)
                {
                    resultSet.close();
                }
            }
            catch (SQLException e)
            {
                throw new RuntimeException(e);
            }
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}

import org.junit.jupiter.api.Test;

import java.sql.*;

public class JDBC
{
@Test
public void TestProperties()
{
Statement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try
{
//JDBC需要配置系统环境变量中,classpath设置.:加上jar包名字以及所在的完整路径,IDEA可以后台完成这部分设置
//JDBC编程6步
//注册驱动Driver,获取链接Connection,获取操作对象Statement,执行SQL语句,处理查询结果集,释放资源
//注册驱动,相当于通知Java程序我们要连接哪个数据库,将Mysql(厂商写的Driver实现类传到DriverManager下)
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获取链接,相当于建立JVM和数据库这两个进程之间的通道,这是重量级的进程之间的通信,结束一定要关闭掉
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:13306/mall","root","abc123");
//URL统一资源定位符,网络中某个资源的绝对路径,网址就是URL,包括了协议(HTTP://),IP(182.61.200.7),PORT(80),资源名(index.html)
//同理可得,URL为jdbc:mysql://为协议,localhost为IP地址,端口号为13306,资源为mall
//什么是通信协议?有什么用,通信协议是通信之前提前定好的数据传输协议,数据包具体怎么传输,要遵循提前定好的格式
statement = connection.createStatement();
String sql = "insert into users(username,pssword,email) values('Susan','123456','123@qq.com')";
//专门执行DML语句的,返回影响数据库中的记录条数
int i = statement.executeUpdate(sql);
resultSet = statement.executeQuery("select * from users");
System.out.println(i == 1 ? "保存成功" : "保存失败");
while (resultSet.next())
{
String username = resultSet.getString("username");
String password = resultSet.getString("pssword");
String email = resultSet.getString("email");
System.out.println(username);
System.out.println(password);
System.out.println(email);
}
//获取数据库操作对象,专门用于执行SQL语句的对象
//执行SQL语句
//处理查询结果集,只有第四步是Select语句才有处理结果集
//释放资源(两者之间属于进程间通信),防止内存泄漏
}
catch(SQLException e)
{
e.printStackTrace();
}
finally
{
//释放资源一定要分别对其进行try..catch操作,避免无法关闭成功
try
{
if(statement != null)
{
statement.close();
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
try
{
if (resultSet != null)
{
resultSet.close();
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
try
{
if (connection != null)
{
connection.close();
}
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值