0
0

More than 1 year has passed since last update.

db select

Last updated at Posted at 2023-02-07

no argument: Statement
one or more argument: PreparedStatement

public class Outer {
    public static void main(String[] args) throws Exception{
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/data/Sample");
        try(con) {
            System.out.println(con);
            String sql = "select * from firsttable where id<=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1,20);
            ResultSet rs = ps.executeQuery();
            ResultSetMetaData md = ps.getMetaData();
            int columnCount = md.getColumnCount();
            System.out.println("col count : " + columnCount);
            
            try(rs) {
                int colIndex = 0;
                while(rs.next()){
                    
                    System.out.print(rs.getString("ID") + ":");
                    System.out.println(rs.getString("NAME"));
                    for(int i=0;i<columnCount;i++) {
                        colIndex = i + 1;
                        System.out.println(" "+ md.getTableName(colIndex) + ":" + 
                                md.getColumnName(colIndex) + ":" +
                                rs.getString(colIndex));
                    }
                }
            }
        }
    }
}
org.apache.derby.client.net.NetConnection@17d0685f
org.apache.derby.client.net.NetConnection@17d0685f
col count : 2
10:TEN
 FIRSTTABLE:ID:10
 FIRSTTABLE:NAME:TEN
20:TWENTY
 FIRSTTABLE:ID:20
 FIRSTTABLE:NAME:TWENTY
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0