sql如何使用JDBC将列表或集合中的项目插入表中?
要将数据库的内容插入到集合中,
连接到数据库,并使用SELECT查询将表的内容检索到ResultSet对象中。
DriverManager.registerDriver(new com.mysql.jdbc.Driver()); String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from MyPlayers");
创建一个Java类来保存每个记录的内容,并为每个列(具有适当的数据类型)使用变量以及setter和getter方法。
例如,如果数据库中的表样本具有两个包含详细信息的字段,则:
列名:ID,数据类型:INT(11)
列名称:名称,数据类型VARCHAR(255)
然后,该类中的变量声明以及setter和getter方法将类似于-
import java.sql.Date; public class Sample { private int ID; private String Name; public int getID() { return ID; } public void setID(int id) { ID = id; } public int getName() { return Name; } public void setName(int name) { Name = name; } }
对于每个记录,创建上述类(示例)的对象,从ResultSet对象检索每一列的值,然后使用其setter方法将其设置为该对象,如-
Player player = new Player(); player.setID(rs.getInt("ID")); player.setName(rs.getString("Name"));
将对象添加到所需的集合。
ArrayList<Player> arrayList = new ArrayList<>(); arrayList.add(player);
让我们使用CREATE语句在MySQL数据库中创建一个名为MyPlayers 的表,如下所示-
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用INSERT语句在MyPlayers 表中插入7条记录-
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
使用的Java类保存每个表的数据-
import java.sql.Date; public class Player { private int ID; private String First_Name; private String Last_Name; private Date Date_Of_Birth; private String Place_Of_Birth; private String Country; public int getID() { return ID; } public void setID(int id) { ID = id; } public String getFirst_Name() { return First_Name; } public void setFirst_Name(String first_Name) { First_Name = first_Name; } public String getLast_Name() { return Last_Name; } public void setLast_Name(String last_Name) { Last_Name = last_Name; } public Date getDate_Of_Birth() { return Date_Of_Birth; } public void setDate_Of_Birth(Date date_Of_Birth) { Date_Of_Birth = date_Of_Birth; } public String getPlace_Of_Birth() { return Place_Of_Birth; } public void setPlace_Of_Birth(String place_Of_Birth) { Place_Of_Birth = place_Of_Birth; } public String getCountry() { return Country; } public void setCountry(String country) { Country = country; } }
下面的JDBC程序检索MyPlayers表的内容,将它们添加到ArrayList中。
示例
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; public class TableToArrayList { public static void main(String args[]) throws Exception { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //创建一个Statement对象 Statement stmt = con.createStatement(); //检索数据 ResultSet rs = stmt.executeQuery("select * from MyPlayers"); //创建一个ArrayList对象 ArrayList<Player> arrayList = new ArrayList<>(); //将表的记录添加到数组列表 while(rs.next()) { Player player = new Player(); player.setID(rs.getInt("ID")); player.setFirst_Name(rs.getString("First_Name")); player.setLast_Name(rs.getString("Last_Name")); player.setDate_Of_Birth(rs.getDate("Date_Of_Birth")); player.setPlace_Of_Birth(rs.getString("Place_Of_Birth")); player.setCountry(rs.getString("Country")); arrayList.add(player); } for (Player obj : arrayList) { System.out.print("ID: "+obj.getID()+", "); System.out.print("Name: "+obj.getFirst_Name()+", "); System.out.print("Age: "+obj.getLast_Name()+", "); System.out.print("Salary: "+obj.getDate_Of_Birth()+", "); System.out.print("Country: "+obj.getPlace_Of_Birth()+", "); System.out.print("Address: "+obj.getCountry()); System.out.println(); } } }
输出结果
Connection established...... Records added to the array list..... Contents of the array list: ID: 1, Name: Shikhar, Age: Dhawan, Salary: 1981-12-05, Country: Delhi, Address: India ID: 2, Name: Jonathan, Age: Trott, Salary: 1981-04-22, Country: CapeTown, Address: SouthAfrica ID: 3, Name: Kumara, Age: Sangakkara, Salary: 1977-10-27, Country: Matale, Address: Srilanka ID: 4, Name: Virat, Age: Kohli, Salary: 1988-11-05, Country: Mumbai, Address: India ID: 5, Name: Rohit, Age: Sharma, Salary: 1987-04-30, Country: Nagpur, Address: India ID: 6, Name: Ravindra, Age: Jadeja, Salary: 1988-12-06, Country: Nagpur, Address: India ID: 7, Name: James, Age: Anderson, Salary: 1982-06-30, Country: Burnely, Address: England