JAVA使用JDBC对数据库的内容进行insert操作—–JAVA入门基础教程

  • Post author:
  • Post category: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);
                }
            }
        }
    }
}



版权声明:本文为2201_75960169原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。