此处代码的目的是通过选择列来读取记录。
我在SQL DB中有以下列。
------------------------------
|id | Topic | Comment | Time |
------------------------------
我想做的是: -
主题可以重复,但是在选择过程中不应该看到重复的值,但应检索所有列的值(以及重复的值)。
public class ReadRecordTopic{
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/STUDENTS?user=root&password=";
// Database credentials
//static final String USER = "username";
//static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating statement..." + "\n");
stmt = conn.createStatement();
String sql = "SELECT id, Topic, Comment, Time FROM Registration";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
String Topic = (String) rs.getString("Topic");
String Comment = rs.getString("Comment");
String Time = rs.getString("Time");
String menu [] = Topic.split(",");
Object[] selectionValues = menu;
String initialSelection = "";
Object selection = JOptionPane.showInputDialog(null,
"Please select the Topic.", "Reseach Forum Menu",
JOptionPane.QUESTION_MESSAGE, null, selectionValues,
initialSelection);
String a = "Research Topic: " + Topic + "\n";
String b = "[" + Time + "]" + " Comment:" + Comment + "\n";
if(selection == Topic){JOptionPane.showMessageDialog(null, a + b);
}
}
rs.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
答案 0 :(得分:0)
1-创建Registration.class
public class Registration{
private int id;
private String topic;
...
...
...
//getters and setters
}
2-更改您的sql和函数名称以了解您想要的内容并使用它。
//for order topic
String sql = SELECT id, Topic, Comment, Time FROM Registration order by topic.
//function for just spesific topic
public class ArrayList<Registration> getRegisWithTopic(String topicName){
String sql = SELECT id, Topic, Comment, Time FROM Registration order by topic WHERE TOpic = topicName ;
}
3- in in create new Registration.class并添加到ArrayList并返回此列表
ArrayList<Registration> newList = new ArrayList<Registration>();
while(rs.next()){
Registration newReg = new Registration();
//Retrieve by column name
newReg.setId(rs.getInt("id"));
newReg.setTopic(rs.getString("Topic"));
...
...
...
//add this obj to list
newList.add(newReg);
}
return newList ;