java连接MySql数据库!
2004-12-18 14:48
489 查看
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
package com.roytel.rtccp.util;
import java.sql.*;
public class DBManager {
//用户名
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;
/**
* 根据主机、数据库名称、数据库用户名、数据库用户密码取得连接。
* @param host String
* @param database String
* @param user String
* @param password String
*/
public DBManager(String host, String database, String user, String password) {
this.host = host;
this.database = database;
this.user = user;
this.password = password;
//显示中文
this.url = "jdbc:mysql://" + host + "/" + database +
"?useUnicode=true&characterEncoding=GB2312";
try {
Class.forName("org.gjt.mm.mysql.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:"
4000
+ a.getMessage());
}
}
/**
* 返回取得的连接
*/
public Connection getCon() {
return con;
}
/**
* 执行一条简单的查询语句
* 返回取得的结果集
*/
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
public boolean executeUpdate(String sql) {
boolean v = false;
try {
v = stmt.executeUpdate(sql) > 0 ? true : false;
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
return v;
}
}
}
能用的,你可以扩充,比如增加执行预编译语句的方法、执行存储过程的方法,也可以用连接池的方法。
package com.roytel.rtccp.util;
import java.sql.*;
public class DBManager {
//用户名
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;
/**
* 根据主机、数据库名称、数据库用户名、数据库用户密码取得连接。
* @param host String
* @param database String
* @param user String
* @param password String
*/
public DBManager(String host, String database, String user, String password) {
this.host = host;
this.database = database;
this.user = user;
this.password = password;
//显示中文
this.url = "jdbc:mysql://" + host + "/" + database +
"?useUnicode=true&characterEncoding=GB2312";
try {
Class.forName("org.gjt.mm.mysql.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:"
4000
+ a.getMessage());
}
}
/**
* 返回取得的连接
*/
public Connection getCon() {
return con;
}
/**
* 执行一条简单的查询语句
* 返回取得的结果集
*/
public ResultSet executeQuery(String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
public boolean executeUpdate(String sql) {
boolean v = false;
try {
v = stmt.executeUpdate(sql) > 0 ? true : false;
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
return v;
}
}
}
能用的,你可以扩充,比如增加执行预编译语句的方法、执行存储过程的方法,也可以用连接池的方法。
相关文章推荐
- Java连接MySQL数据库并查询结果
- Java使用JDBC连接mySQL数据库小例子
- Java连接Mysql数据库警告:Establishing SSL connection without server's identity
- java读取properties文件,通过数据池(BasicDataSource)连接mysql数据库
- JDBC之java连接mysql数据库的实现
- java语言连接Mysql数据库
- java 通过JDBC连接Mysql数据库的方法和实例
- JAVA实现新浪SAE上 mysql数据库的连接
- java连接mysql数据库
- Java 建立mysql数据库连接的语句
- java连接mysql数据库 代码
- 【JAVA】java连接MySql数据库
- Java连接Mysql数据库代码
- java连接mySQL数据库
- java连接mysql数据库增删改查操作记录
- Java基于jdbc连接mysql数据库操作示例
- java web 连接mysql数据库
- java连接mysql数据库详细步骤解析
- java连接Mysql数据库
- 纪念第一次java连接mysql数据库