您好我正在创建一个应用程序,我可以在其中将数据存储在购物车模块的 Hashmap 和列表中。但我想从我的 MS-Access 数据库中获取这些数据。我尝试了以下代码,但它没有编译。请给我指导.
代码:
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Program {
public static void main(String [] args){}
public static HashMap getProductsAsMap() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pd");
ResultSet rs = null;
Statement st = con.createStatement();
String sql = ("select * from products");
rs=st.executeQuery(sql);
while (rs.next()) {
HashMap<String, ProductBean> products= new HashMap<String, ProductBean>();
String name=rs.getString("pname");
String desc=rs.getString("pdesc");
String image=rs.getString("pimage");
products.put("P1", new ProductBean(name,desc,image));
return products;
}
rs.close();
st.close();
con.close();
}
catch(Exception e){}
}
public static List getProductsAsList() {
List<ProductBean> products = new ArrayList<ProductBean>();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:pd");
ResultSet rs = null;
Statement st = con.createStatement();
String sql = ("select * from products");
rs=st.executeQuery(sql);
while (rs.next()) {
String name=rs.getString("pname");
String desc=rs.getString("pdesc");
String image=rs.getString("pimage");
products.add(new ProductBean(name,desc,image));
}
rs.close();
st.close();
con.close();
}
catch(Exception e){}
return products;
}
}
我得到2个错误如下: