3.写一段JDBC连接数据库的代码,CONNECTION最后要关闭。
2个回答
展开全部
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
return DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=test","sa","");
} catch (ClassNotFoundException e) {
throw new DaoException("加载驱动程序失败",e);
} catch (SQLException e) {
throw new DaoException("数据库连接失败",e);
}
}
public void close(ResultSet rs, Statement stmt, Connection conn) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
return DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=test","sa","");
} catch (ClassNotFoundException e) {
throw new DaoException("加载驱动程序失败",e);
} catch (SQLException e) {
throw new DaoException("数据库连接失败",e);
}
}
public void close(ResultSet rs, Statement stmt, Connection conn) {
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Conn {
private Connection connection = null;
static String username = "scott";
static String password = "tiger";
static String classStr = "oracle.jdbc.driver.OracleDriver";
static String urlStr = "jdbc:oracle:thin:@localhost:1521:myoracle";
public Conn() throws ClassNotFoundException, SQLException {
Class.forName(classStr);
connection = DriverManager.getConnection(urlStr,
username, password);
}
public Connection getConnection() {
return connection;
}
/*这是一个专门用来关闭程序中所有Connection的静态方法,包括其子接口,下面有关于对这个方法的调用类的举例*/
public static void close(ResultSet resultSet,
PreparedStatement prepraredStatement,
CallableStatement callableStatement, Statement statement,
Connection connection) throws SQLException {
if (resultSet != null){
resultSet.close();
}
if (prepraredStatement != null){
prepraredStatement.close();
}
if (callableStatement != null){
callableStatement.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}
public class Database {
private Connection connection = null;
public Database() throws ClassNotFoundException, SQLException {
connection = new Conn().getConnection();
}
public int login(LoginDTO loginDTO) throws SQLException {
int flag = 0;
String sqlStr = "begin ?:=useroption.login(?,?); end;";
CallableStatement callableStatement = connection.prepareCall(sqlStr);
callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
callableStatement.setString(2, loginDTO.getJTextField_username());
callableStatement.setString(3, loginDTO.getJPasswordField_password());
callableStatement.execute();
flag = callableStatement.getInt(1);
Conn.close(null, null, callableStatement, null, connection);
return flag;
}
private Connection connection = null;
static String username = "scott";
static String password = "tiger";
static String classStr = "oracle.jdbc.driver.OracleDriver";
static String urlStr = "jdbc:oracle:thin:@localhost:1521:myoracle";
public Conn() throws ClassNotFoundException, SQLException {
Class.forName(classStr);
connection = DriverManager.getConnection(urlStr,
username, password);
}
public Connection getConnection() {
return connection;
}
/*这是一个专门用来关闭程序中所有Connection的静态方法,包括其子接口,下面有关于对这个方法的调用类的举例*/
public static void close(ResultSet resultSet,
PreparedStatement prepraredStatement,
CallableStatement callableStatement, Statement statement,
Connection connection) throws SQLException {
if (resultSet != null){
resultSet.close();
}
if (prepraredStatement != null){
prepraredStatement.close();
}
if (callableStatement != null){
callableStatement.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
}
public class Database {
private Connection connection = null;
public Database() throws ClassNotFoundException, SQLException {
connection = new Conn().getConnection();
}
public int login(LoginDTO loginDTO) throws SQLException {
int flag = 0;
String sqlStr = "begin ?:=useroption.login(?,?); end;";
CallableStatement callableStatement = connection.prepareCall(sqlStr);
callableStatement.registerOutParameter(1, java.sql.Types.INTEGER);
callableStatement.setString(2, loginDTO.getJTextField_username());
callableStatement.setString(3, loginDTO.getJPasswordField_password());
callableStatement.execute();
flag = callableStatement.getInt(1);
Conn.close(null, null, callableStatement, null, connection);
return flag;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询