java类——操作MySql数据库
来源:互联网 发布:js特效网站 编辑:程序博客网 时间:2024/04/30 15:01
package tt;
import java.sql.*;
public class DB_mysql {
private String user = "";//用户名
private String password = "";//密码
private String host = "";//主机
private String database = "";//数据库名字
/*
private String url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
*/
private String url ="";
private Connection con = null;
Statement stmt;
public DB_mysql(String host, String database, String user, String password) {
this.host = host;
this.database = database;
this.user = user;
this.password = password;
//显示中文
this.url = "jdbc:mysql://" + this.host + "/" + this.database +
"?useUnicode=true&characterEncoding=GB2312";
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.err.println("class not found:" + e.getMessage());
}
try {
con = DriverManager.getConnection(this.url, this.user, this.password);
//连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException a) {
System.err.println("sql exception:" + a.getMessage());
}
}
public DB_mysql(String database) {//重载,直接连接默认数据库系统,只选择数据库名称
this.host = "localhost";
this.database = database;
this.user = "root";
this.password = "";
//显示中文
this.url = "jdbc:mysql://" + host + "/" + database +
"?useUnicode=true&characterEncoding=GB2312";
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.err.println("class not found:" + e.getMessage());
}
try {
con = DriverManager.getConnection(this.url, this.user, this.password);
//连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
catch (SQLException a) {
System.err.println("sql exception:" + a.getMessage());
}
}
/**
* 返回取得的连接
*/
public Connection getCon() {
return con;
}
/**
* 执行一条简单的查询语句
* 返回取得的结果集
*/
public ResultSet exeQuery(String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
public boolean exeUpdate(String sql) {
boolean v = false;
try {
v = stmt.executeUpdate(sql) > 0 ? true : false;
}
catch (SQLException e) {
e.printStackTrace();
}
return v;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
public boolean exeInsert(String sql) {
boolean v = false;
try {
v = stmt.execute(sql) ;
}
catch (SQLException e) {
e.printStackTrace();
}
return v;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
public boolean close() {
boolean v = false;
try{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(con!=null){
con.close();
con=null;
}
}
catch(Exception ee){
System.out.println("数据库无法关闭,请检查!");
ee.printStackTrace();
}
return v;
}
}
- java类——操作MySql数据库
- MySQL+Java——数据库查询操作
- JAVA操作MySQL数据库类
- JAVA操作MySQL数据库类
- java操作数据库MYSQL
- java操作mysql数据库
- java操作mysql数据库
- JAVA操作mysql数据库
- java 操作mysql数据库
- java操作mysql数据库
- JAVA 数据库操作工具类----mysql
- Java操作Mysql数据库辅助类
- .Net — MySql数据库操作
- MySQL数据库及Java操作MySQL数据库
- MySQL数据库学习——数据库操作
- MySQL数据库—使用命令行操作数据库
- 【Java】【MySQL】Java操作mysql数据库基本步骤介绍——从零开始#1
- java 数据库操作(mysql)
- 绕过安全软件挂钩SSDT的检测
- java类——使用dom4j创建xml文件
- JSP 如何在客户端同步显示服务器时间
- java类——操作Access数据库
- 调侃国足
- java类——操作MySql数据库
- oracle to_char() to_date()函数学习
- java类——操作SqlServer数据库
- TC35T 猫开发笔记(1)
- 对Java新员工培训练习的评价:玩具
- java类——下载文件
- 項目佈署中遇到的問題
- SVN Apache2 配置
- decode的使用