package conn; //导入包import java.sql.*;
//导入数据库操作的类public class DBConnMySql
//构造方法,初始化{private Connection conn;
//连接对象private Statement stmt;
//语句对象private ResultSet rs;
//结果集对象private String MySqldriver;
//MYSQL Server驱动程序字符串private String MySqlURL;
//MYSQL Server连接字符串//*********************************
用 org.gjt.mm.mysql.Driver 驱动
* 该方法取得连接所需各种参数,组成连接字符串,
然后再建立连接* server;dbname,user,pass,port
分别表示MYSQL 服务器的地址,
* 数据库,用户名,密码,端口
**********************************/
public Connection getConnToMySql
(String server,String dbname,String user,String pass,String port)
{//MYSQl驱动程序MySqldriver = "org.gjt.mm.mysql.Driver";
MySqlURL = "jdbc:mysql://";
//连接字符串一部分try{//完整的连接字符串MySqlURL
=MySqlURL+server+":"+port+"/"+dbname+"?user=
"+user+"&password="+pass+"&useUnicode
=true&characterEncoding=GB2312";
Class.forName(MySqldriver);conn
= DriverManager.getConnection(MySqlURL);}
catch(Exception e){System.out.println
("操作数据库出错,请仔细检查");
//System.err.println(e.getMessage());}return conn;}
//关闭数据库连接public void close(){try{//rs.close();
//stmt.close();conn.close();}catch(SQLException
sqlexception){sqlexception.printStackTrace();}}}