import java.sql.*;public class MysqlBean { private Connection con = null; private ResultSet rs; private String driverName = "com.mysql.jdbc.Driver"; private String serverName = "localhost"; private String mydatabase = "user"; private String username = "root"; private String password = "138717"; public MysqlBean() { try { String url = "jdbc:mysql://" + serverName + "/" + mydatabase; // a Class.forName(driverName); // con = DriverManager.getConnection(url, username, password); con = DriverManager.getConnection("jdbc:mysql://" + serverName + ":3306/" + mydatabase + "?user=" + username + "&password=" + password + "&useUnicode=true&characterEncoding=UTF-8"); // con.setAutoCommit(false); } catch (ClassNotFoundException e) { // Could not find the database driver System.out.println("Could not find the database driver"); } catch (SQLException e) { // Could not connect to the database System.out.println("Could not connect to the database"); } } public Connection getCon() { return con; } public ResultSet getRs(String sql) throws Exception { try { Statement stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql); } catch (Exception e) { e.printStackTrace(); } return rs; } public int executeUpdate(String sql) { int count = 0; try { Statement stmt = con .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); count = stmt.executeUpdate(sql); if (count != 0) ; // con.commit(); else ; // con.rollback(); } catch (SQLException ex) { ex.printStackTrace(); } return count; } public void Close() { try { if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } }}
详细解决方案
联接mysql用的JavaBean
热度:123 发布时间:2016-05-05 17:05:58.0
相关解决方案
- mysql 简单化 or能否实现
- 请问registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister
- MySQL 5.5 Command Line Client 窗口1输密码就退出
- 请问上Linux平台上怎么搭建JDK \TOMCAT\MYSQL
- 在LINUX上配置 MySQL 开机自动 启动
- mysql 转 orocle java ssh项目一条 sql 句不通!
- 急求帮忙!mysql 【 Column count doesn't match value count 】,该怎么解决
- jsp中的注册登录系统(mysql)
- 上了个 MySQL 5.5.25 但是安装时出错了
- 求jsp博客源代码mvc+mysql,该如何解决
- java mysql 中文乱码有关问题
- jsp+javabean+servlet学习中的一点疑惑,请路过的们解惑
- 请教各位,使用PreparedStatement mysql 数据库 不回滚,盼望解答。多谢。 具体如上
- mysql Statement parameter 一 not set
- java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306,该怎么处理
- MyBatis 读取 Mysql Blob类型的SQL如何写呢
- JDBC MYSQL 驱动加载失败 JSP DAO ECLIPSE,该怎么解决
- Only a type can be imported. com.mysql.jdbc.Driver resolves to a package解决方案
- tomcat 中抛异com.mysql.jdbc.exceptions.MySQLTransactionRollbackException,该如何处理
- javaBean 里面获取系统时间出错解决办法
- struts+iBatis+mysql,该如何解决
- mysql 有外键的插入解决方案
- JSP 更新 MySQL 语句时遇到异常了= =
- javabean+mysql做登陆有关问题
- mysql,该如何处理
- +++++ mysql 插入成功,查询不到记录?
- MyBatis3.1.1 Insert 回到主键 long类型 MYSQL 数据库
- mysql:假若一个表中,有两个属性name和id,删除同名的保留id小的,问这样写有误吗
- java mysql where限制有关问题
- mysql 数据库,如果信息存在调出,如果不存在转到另一个jsp中解决思路