实践1
实践2
public class MessageDAO {
public static String url;
private String user;
private String passwd;
public MessageDAO(String url, String user, String passwd) {
this.url = url;
this.user = user;
this.passwd = passwd;
}
public void add(week12.Message message) {
try(Connection conn = DriverManager.getConnection(url, user, passwd);
Statement statement = conn.createStatement()) {
String sql = String.format(
"INSERT INTO t_message(name, CountryCode, Distridt,Population) VALUES ('%s', '%s', '%s', '%s')",
message.getName(), message.getCountryCode(), message.getDistridt(),message.getPopulation());
statement.executeUpdate(sql);
} catch(SQLException ex) {
throw new RuntimeException(ex);
}
}
public static week12.Message toMessage(ResultSet result) throws SQLException {
week12.Message message = new week12.Message();
message.setId(result.getLong(1));
message.setName(result.getString(2));
message.setCountryCode(result.getString(3));
message.setDistridt(result.getString(4));
message.setPopulation(result.getString(4));
return message;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/world";
String user = "root";
String passwd = "root";
try (Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
try (Statement statement = conn.createStatement()) {
ResultSet result =
statement.executeQuery("SELECT * FROM `city` WHERE Population>5000000");
System.out.println("ID" + " " + " " + "Name" + " " + " " + "CountryCode" + " " + " " + "Distridt" + " " + " " + "Population");
int count = 0;
while (result.next()) {
System.out.print(result.getInt(1) + " "+" ");
System.out.print(result.getString(2) + " "+" ");
System.out.print(result.getString(3) + " "+" ");
System.out.print(result.getString(4) + " "+" ");
System.out.print(result.getString(5) + " "+" ");
System.out.println();
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}}
实践三
import java.sql.*;
import java.util.*;
public class MessageDAO {
public static String url;
private String user;
private String passwd;
public MessageDAO(String url, String user, String passwd) {
this.url = url;
this.user = user;
this.passwd = passwd;
}
public void add(Message message) {
try(Connection conn = DriverManager.getConnection(url, user, passwd);
Statement statement = conn.createStatement()) {
String sql = String.format(
"INSERT INTO t_message(name, CountryCode, Distridt,Population) VALUES ('%s', '%s', '%s', '%s')",
message.getName(), message.getCountryCode(), message.getDistridt(),message.getPopulation());
statement.executeUpdate(sql);
} catch(SQLException ex) {
throw new RuntimeException(ex);
}
}
public static Message toMessage(ResultSet result) throws SQLException {
Message message = new Message();
message.setId(result.getLong(1));
message.setName(result.getString(2));
message.setCountryCode(result.getString(3));
message.setDistridt(result.getString(4));
message.setPopulation(result.getString(4));
return message;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/world";
String user = "root";
String passwd = "root";
try (Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
try (Statement statement = conn.createStatement()) {
ResultSet result =
statement.executeQuery("SELECT SUM( Population ) FROM `city` WHERE District = 'Tokyo'");
System.out.println("ID" + " " + " " + "Name" + " " + " " + "CountryCode" + " " + " " + "Distridt" + " " + " " + "Population");
int count = 0;
while (result.next()) {
System.out.print(result.getString(1) + " "+" ");
System.out.println();
}
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}}
实践四
import java.sql.*;
import java.util.*;
public class MessageDAO {
public static String url;
private String user;
private String passwd;
public MessageDAO(String url, String user, String passwd) {
this.url = url;
this.user = user;
this.passwd = passwd;
}
public void add(Message message) {
try(Connection conn = DriverManager.getConnection(url, user, passwd);
Statement statement = conn.createStatement()) {
String sql = String.format(
"INSERT INTO t_message(LifeExpectancy) VALUES ('%s')",message.getLifeExpectancy()
);
statement.executeUpdate(sql);
} catch(SQLException ex) {
throw new RuntimeException(ex);
}
}
public static Message toMessage(ResultSet result) throws SQLException {
Message message = new Message();
message.setLifeExpectancy(result.getString(1));
return message;
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://localhost:3306/world";
String user = "root";
String passwd = "root";
try (Connection conn =
DriverManager.getConnection(jdbcUrl, user, passwd)) {
System.out.printf("已%s数据库连接%n", conn.isClosed() ? "关闭" : "打开");
try (Statement statement = conn.createStatement()) {
ResultSet result =
statement.executeQuery("SELECT * FROM `country` WHERE 1");
int count = 0;
Double sum=0.0;
while (result.next()) {
System.out.print( result.getDouble(8)+ " "+" ");
sum=sum+ result.getDouble(8);
System.out.println();
count++;
}
System.out.println(sum/count);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}}