Java连接数据库

package dataku;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DbConnection {
private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String username = "system";
private static final String password = "tiger";
private static final String driver = "oracle.jdbc.driver.OracleDriver";
private Connection conn = null;
/**
* 获取数据库连接
* @return
*/
public Connection getConn(){

try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
/**
* 关闭数据库
*/
public void close(){
if(conn != null )
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

 

你可能感兴趣的:(Java连接数据库)