import java.sql.*; public class Select { public static void main(String[] args) { Connection connection = 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"; connection = DriverManager.getConnection(url,user,password); System.out.println(connection); statement = connection.createStatement(); String sql = "SELECT * FROM countries"; ResultSet resultSet = statement.executeQuery(sql); while(resultSet.next()) { String country_id = resultSet.getString("country_id"); String country_name = resultSet.getString("country_name"); int region_id = resultSet.getInt("region_id"); System.out.println(country_id + "\t" + country_id + "\t" + region_id); } } catch (SQLException e) { throw new RuntimeException(e); } finally { try { connection.close(); statement.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
import java.sql.*;
public class Select
{
public static void main(String[] args)
{
Connection connection = 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";
connection = DriverManager.getConnection(url,user,password);
System.out.println(connection);
statement = connection.createStatement();
String sql = "SELECT * FROM countries";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next())
{
String country_id = resultSet.getString("country_id");
String country_name = resultSet.getString("country_name");
int region_id = resultSet.getInt("region_id");
System.out.println(country_id + "\t" + country_id + "\t" + region_id);
}
} catch (SQLException e)
{
throw new RuntimeException(e);
}
finally
{
try
{
connection.close();
statement.close();
} catch (SQLException e)
{
throw new RuntimeException(e);
}
}
}
}
JAVA使用JDBC执行select操作数据库的内容-----JAVA
于 2023-05-25 19:15:24 首次发布