把jar放到jdk的lib目录下
import java.sql.*;
import java.io.*;
public class OpenDB
{
Connection dbConn;
String host="localhost";
String database="netshop";
String userName="root";
String userPassword="****";//此处的密码用户名一定要与所用数据库的密码及用户名一致
private String driverName = "org.gjt.mm.mysql.Driver";
private String url= "jdbc:mysql://"+host+"/"+database+"?useUnicode=true&characterEncoding=gb2312";
public OpenDB()
{
}
public Connection getConnection()
{
try
{
Class.forName(driverName).newInstance();
dbConn=DriverManager.getConnection(url,userName,userPassword);
}
catch(Exception ex)
{
System.out.println(ex.toString());
dbConn = null;
System.out.println("unable to load driver");
}
return dbConn;
}
}