package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DButil {
private static Connection conn=null;
static{
try {
Class.forName("com.jdbc.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
return conn;
}
}
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import model.Goddess;
import util.DButil;
public class GoddessDao {
public void addGoddess() {
}
public void updateGoddess() {
}
public void deleteGoddess(){
}
public List<Goddess> queryMore(){
Connection conn= DButil.getConnection();
List<Goddess> goddessesList = new ArrayList<Goddess>();
Statement statement;
try {
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select name,age from imooc_jdbc_goddess");
while (rs.next()) {
Goddess goddess = new Goddess();
goddess.setUser_name(rs.getString("user_name"));
goddess.setAge(rs.getInt("age"));
goddessesList.add(goddess);
}
} catch (SQLException e) {
e.printStackTrace();
}
return goddessesList;
}
public void querySingle(){
}
}
package action;
import java.sql.SQLException;
import java.util.List;
import dao.GoddessDao;
import model.Goddess;
public class Action {
public static void main(String[] args) throws SQLException {
GoddessDao gd = new GoddessDao();
List<Goddess> gList = gd.queryMore();
for (Goddess goddess : gList) {
System.out.println(goddess.getUser_name()+";"+goddess.getAge());
}
}
}