jdbc查询数据库案例1

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //2.获得数据库连接
        String url="jdbc:oracle:thin:@localhost:1521:orcl";
        Connection conn=DriverManager.getConnection(url,"system","toor");
       System.out.println("连接成功");
        //3.创建Statement
  Statement st=conn.createStatement();
  //执行sql
     ResultSet rs=st.executeQuery("select * from student");
     //遍历
     while(rs.next()){
         System.out.print(rs.getInt(1)+"\t");
         System.out.print(rs.getString(2)+"\t");
         System.out.println(rs.getString(3));
     }
       st.close();
        conn.close();
    }

}

 

posted @ 2017-05-20 14:44  苏轼的红烧肉  阅读(151)  评论(0编辑  收藏  举报