基于mysql的数据库增删改查

Until

import java.sql.*;

public class Util
{
    public static Connection getConnection() throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 2.创建连接
        Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/zhong","root","root");
        System.out.println("ok");
        return  connection;


    }
    public static  void closeAll(ResultSet resultSet,Statement statement,Connection connection) throws SQLException
    {
        if (resultSet!=null)
        {
            resultSet.close();
        }
        if (statement!=null)
        {
            statement.close();
        }
        if (connection!=null)
        {
            connection.close();
    }

    }
}

Find

import java.sql.*;

public class Find
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try
        {
            Util util=new Util();
            connection=util.getConnection();
            //3.写sql语句
            String sql="select * from user";

            //4.获得statement
            ps=connection.prepareStatement(sql);
            //5.执行sql,得到结果集
            rs=ps.executeQuery();
            //6.处理结果集
            while (rs.next())
            {
                System.out.print(rs.getInt(1)+" ");
                System.out.print(rs.getString(2)+" ");
                System.out.print(rs.getString(3));
                System.out.println(" ");
            }

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //7.关闭资源
            Util.closeAll(rs,ps,connection);
        }

    }

}

Add

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Add
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        try
        {
            //1.加载驱动
            connection=Util.getConnection();
            //3.写sql语句
            String sql="insert into user(username,password) values ('dd','123')";
            //4.获得statement
            statement=connection.prepareStatement(sql);
            //5.执行sql,得到结果集
            statement.executeUpdate();
            //6.处理结果集

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //7.关闭资源
            Util.closeAll(rs,statement,connection);
        }

    }
}

Delete

import java.sql.*;

public class Delete
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException
    {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        try
        {
            //1.加载驱动
            connection=Util.getConnection();
            //3.写sql语句
            String sql="delete from user where username='ddd'";
            //4.获得statement
            statement=connection.prepareStatement(sql);
            //5.执行sql,得到结果集
            statement.executeUpdate();
            //6.处理结果集

        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //7.关闭资源
            Util.closeAll(rs,statement,connection);
        }
    }
}

Update

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Update
{
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet rs=null;
        try
        {
            //1.加载驱动
            connection=Util.getConnection();
            //3.写sql语句
            String sql="update user set username='ddd' where username='dd'";
            //4.获得statement
            statement=connection.prepareStatement(sql);
            //5.执行sql,得到结果集
            statement.executeUpdate();
            //6.处理结果集
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            //7.关闭资源
            Util.closeAll(rs,statement,connection);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值