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使用JDBC对数据库的内容进行insert操作-----JAVA
于 2023-05-20 13:32:32 首次发布