Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.myapp.jdbc;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.sql.rowset.JdbcRowSet;
- import com.myapp.generics.Customer;
- import com.sun.rowset.JdbcRowSetImpl;
- public class TestJdbcRowSet {
- JdbcRowSet rowset=null;
- public TestJdbcRowSet() {
- rowset=new JdbcRowSetImpl();
- try {
- rowset.setUrl("jdbc:mysql://localhost:3306/citrix");
- rowset.setUsername("admin");
- rowset.setPassword("admin");
- rowset.setCommand("select * from customer");
- rowset.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public List<Customer> showCustomers(){
- try{
- ResultSetMetaData metadata=rowset.getMetaData();
- int noOfCols=metadata.getColumnCount();
- List<Customer> list=new ArrayList<>();
- while(rowset.next()){
- //for(int i=1;i<=noOfCols;i++){
- list.add(new Customer(
- rowset.getInt(1),
- rowset.getString(2),rowset.getInt(3)));
- //}
- }
- return list;
- }catch(SQLException e){
- e.printStackTrace();
- }
- return null;
- }
- public void addCustomer(Customer customer){
- try {
- rowset.moveToInsertRow();
- rowset.updateInt(1,customer.getId());
- rowset.updateString(2,customer.getName());
- rowset.updateInt(3,customer.getAge());
- rowset.insertRow();
- rowset.beforeFirst();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- TestJdbcRowSet rowset=new TestJdbcRowSet();
- rowset.addCustomer(new Customer(4,"SomeName4",100));
- List<Customer> list=rowset.showCustomers();
- list.forEach(c->System.out.println(c));
- }
- }
Add Comment
Please, Sign In to add comment