使用jdbc连接oracle数据库Java类:
import java.sql.*;
public class DBHelper {
public static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
public static String username = "vitelon";
public static String pwd = "vitelon";
public static Connection conn = null;
public static PreparedStatement ps = null;
public static ResultSet rs = null;
/**
* 连接数据库
*
* @return
*/
public static Connection getConnection() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, username, pwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭数据连接
*/
public static void close() {
if (conn != null) {
try {
cnon.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}