JDBC connection to Oracle database and list Traversal

Source: Internet
Author: User

I haven't written any code about database connection for a long time, so I can't even do it. I posted a record.

Database-related object classes:

package com.lliu.JDBC;public class AAA {public AAA(){}public AAA(int ID, String name, String dept){this.setID(ID);this.setName(name);this.setDept(dept);}public int ID;public String name;public String dept;public int getID() {return ID;}public void setID(int iD) {ID = iD;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDept() {return dept;}public void setDept(String dept) {this.dept = dept;}}

Connect to the database and traverse the items:

package com.lliu.JDBC;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class JDBCConnection {public static void main(String[] args){Connection conn = null;PreparedStatement stmt = null;ResultSet rs = null;List<AAA> list =new ArrayList<AAA>();AAA a = new AAA();try{Class.forName("oracle.jdbc.driver.OracleDriver");conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "system", "12345");stmt = conn.prepareStatement("select ID,NAME,DEPT from AAA");rs = stmt.executeQuery();while(rs.next()){list.add(new AAA(rs.getInt(1),rs.getString(2),rs.getString(3)));System.out.println(rs.getString(2));}}catch(Exception e){e.printStackTrace();}finally{if(rs != null){try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stmt != null){try {stmt.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(conn != null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}Iterator<AAA> it = list.iterator();while(it.hasNext()){System.out.println(it.next().ID);}}}

More than enough. Make progress every day. Come on ~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.