package com;
import java.sql.*;
import javax.sql.*;
public class TestJdbc {
/**
* @param args
*/
public static void main(String[] args) {
try{
//1.注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//2.通过驱动管理器获得连接
//格式:jdbc:oracle:thin:@<IP地址>:<端口号,默认是1521>:<sid>
String url="jdbc:oracle:thin:@192.168.1.102:1521:zhangchi";
String dbUsername="zhangchi";
String dbPassword="zhangchi";
Connection conn=DriverManager.getConnection(url,dbUsername,dbPassword);
//3.获得语句对象
Statement stmt=conn.createStatement();
//4.传递语句对象,并返回结果
String sql="select id,number from zhang ";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
//id和number是sql语句中的列名
String id=rs.getString("id");
String number=rs.getString("number");
System.out.println(id+" , "+number);
}
//5.关闭资源
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
} |
|