JAVA使用JDBC对数据库的内容进行insert操作-----JAVA

import java.sql.*;

public class JDBC
{
    public static void main(String[] args)
    {
        Connection conn = null;
        Statement statement= null;
        try
        {
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            String url = "jdbc:mysql://127.0.0.1:13306/ip";
            String user = "root";
            String password = "abc123";
            conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
            statement = conn.createStatement();
            String sql = "insert into countries(country_id,country_name,region_id) values('QW','Canada',2)";
            int i = statement.executeUpdate(sql);
            if(i == 1)
            {
                System.out.println("保存成功");
            }
            else
            {
                System.out.println("失败");
            }
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            if(statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(conn != null)
            {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

import java.sql.*;

public class JDBC
{
    public static void main(String[] args)
    {
        Connection conn = null;
        Statement statement= null;
        try
        {
            Driver driver = new com.mysql.jdbc.Driver();
            DriverManager.registerDriver(driver);
            String url = "jdbc:mysql://127.0.0.1:13306/ip";
            String user = "root";
            String password = "abc123";
            conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
            statement = conn.createStatement();
            String sql = "insert into countries(country_id,country_name,region_id) values('QW','Canada',2)";
            int i = statement.executeUpdate(sql);
            if(i == 1)
            {
                System.out.println("保存成功");
            }
            else
            {
                System.out.println("失败");
            }
        }
        catch (SQLException e)
        {
            throw new RuntimeException(e);
        }
        finally
        {
            if(statement != null)
            {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if(conn != null)
            {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

你可能感兴趣的:(JAVA,java,sql,数据库,maven,jvm,mybatis,spring)