CREATE TABLE info(
iname VARCHAR2(10) NOT NULL,
tel NUMBER(20) NOT NULL
)
DROP TABLE info;
INSERT INTO info VALUES('小王',13300003331);
INSERT INTO info VALUES('小绿',13300003332);
INSERT INTO info VALUES('小子',13300003333);
SELECT * FROM info;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test1 {
public static void main(String[] args) {
ResultSet rs = null;
Statement stmt = null;
Connection connection = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager
.getConnection("jdbc:oracle:thin:@localhost:1521:yanghl",
"scott", "tiger");
stmt = connection.createStatement();
rs = stmt.executeQuery("select * from info");
while (rs.next()) {
String iname = rs.getString("iname");
String tel = rs.getString("tel");
System.out.println(iname + ":" + tel);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}