import java.sql.*;
public class DBTest {
private Statement stmt;
private Connection conn;
ResultSet rs;
int size;
public void DBTest(){
stmt=null;
conn=null;
rs=null;
size=0;
}
public void openConn() throws SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
String user="dectdb";
String pwd="dectdb";
conn=DriverManager.getConnection(url, user, pwd);
}catch(ClassNotFoundException classNotFoundException){
System.out.println("No Driver!");
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException sqlException){
System.out.println(sqlException);
}
return rs;
}
public int executeUpdate(String sql){
stmt=null;
rs=null;
size=0;
try{
stmt=conn.createStatement();
size=stmt.executeUpdate(sql);
}catch(SQLException sqlException){
System.out.println(sqlException);
}
return size;
}
public void closeStmt(){
try{
stmt.close();
}catch(SQLException sqlException){
System.out.println(sqlException);
}
}
public static void main(String[] args){
DBTest dbTest=new DBTest();
String sql="select * from dectuser";
//String sqlUpdate="insert into dectuser(name,sex) values('wang','女')";
try{
dbTest.openConn();
//int size=dbTest.executeUpdate(sqlUpdate);
ResultSet rs=dbTest.executeQuery(sql);
//System.out.println(size);
while(rs.next()){
System.out.println(rs.getString("name"));
}
dbTest.closeStmt();
}catch(SQLException sqlException){
System.out.println(sqlException);
}
}
}
posted on 2008-04-13 18:42
绿茶 阅读(255)
评论(1) 编辑 收藏 所属分类:
DataBase