连接Oracle数据库代码
连接oracle数据库的代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JdbcOracle {
public static Connection conn=null;
public static PreparedStatement pstamt=null;
public static ResultSet rs=null;
private static String driver="oracle.jdbc.driver.OracleDriver";
private final static String url="jdbc:oralce:thin:192.168.0.109@lucs109";
private final static String username="lucs";
private final static String pwd="lucs";
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getConn(){
try {
conn=DriverManager.getConnection(url, username, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("连接失败!");
}
}
public void closeAll(){
try{
if(conn!=null){
conn.close();
}
if(!rs.isClosed()||rs!=null){
rs.close();
}
if(pstamt!=null){
pstamt.close();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public int execAllSql(String sql,Object...trop){
getConn();
int count=0;
try {
pstamt=conn.prepareStatement(sql);
for (int i = 0; i < trop.length; i++) {
try {
pstamt.setObject(i, trop[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count=pstamt.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return count;
}
public static void main(String[] args) {
JdbcOracle j=new JdbcOracle();
j.getConn();
}
}