数据库连接_留着以后copy
2017-05-12 09:09
169 查看
数据库连接_留着以后copy
自己写的东西,每次用到数据库都得敲一遍,懒人一个,写出来等哪天用得到直接copy。
代码块
import java.sql.*; public class BaseDao { private Connection conn; private PreparedStatement ps; private ResultSet rs; // 获得连接 private void getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/tushuguan"; conn = DriverManager.getConnection(url, "root", ""); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } // 关闭 public void close() { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } // 更新---增加,修改,删除 public int executeUpdate(String sql, Object... objects) { try { this.getConnection(); ps = conn.prepareStatement(sql); if (objects != null)// 设置参数 for (int i = 0; i < objects.length; i++) { ps.setObject(i + 1, objects[i]); } return ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { this.close(); } return -1; } //查询 public ResultSet executeQuery(String sql,Object...objects){ try { this.getConnection(); ps = conn.prepareStatement(sql); if (objects != null)// 设置参数 for (int i = 0; i < objects.length; i++) { ps.setObject(i + 1, objects[i]); } return ps.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return null; } }
相关文章推荐
- 在arccatlog中SDE连接上空间数据库以后,能看见很多其他用户的数据,这是为什么?比如SCOTT的表
- php升级到5.3+以后,mysqlnd连接数据库的问题
- ORACLE listener加入地址限制以后,连接数据库超慢
- 通过数据库方式访问excel 2007及其以后(xlsx)文件的连接字符串
- ORACLE listener加入地址限制以后,连接数据库超慢
- Access2007及以后的版本中的数据库连接字符串
- web项目中不适用于java.sql.DriverManager描述的JDBC 4.0 Drivers及以后版本连接数据库是不需要用forName()?
- ORA-00257归档日志写满的解决方法 背景: 在前一篇博客中我们提到了如何启动或关闭oracle的归档(ARCHIVELOG)模式,在我成功设定数据库为归档模式以后, 第二天再次尝试连接数据库,
- (转自java中文网)jdbc连接数据库大全 留着以后用
- EJB项目连接数据库发布配置注意点
- oracle连接数据库测试代码
- sql Developer连接数据库时出现的问题
- Android连接SQLServer详细教程(数据库+服务器+客户端)
- QTP连接数据库解决方法
- 在Dreamweaver中建立数据库连接 (access)
- JDBC连接各种数据库的方法(经典)
- Java连接MYSQL 数据库的连接步骤
- C#连接数据库的四种方法
- Enterprise Library2.0中加密数据库连接字符串