数据库文件存储在mysql安装目录下的data目录.
package org;
import java.sql.*;
public class dbconnection{
private string dbdriver = "org.gjt.mm.mysql.driver";
private string myurl = "jdbc:mysql://localhost:3306/mytest";
private string user = "root";
private string password = " http://lovebeyond19831993.blog.163.com";
connection conn = null;
dbconnection(){ //构造函数
try{
class.forname(dbdriver);
}
catch(exception e){ e.printstacktrace(); }
try{
conn = drivermanager.getconnection(myurl,user,password);
}
catch(sqlexception s){ s.printstacktrace(); }
}
public connection getconnection(){ //返回connectin类型
return conn;
}
public void getclose(){ //关闭数据库连接
try{
this.conn.close();
}
catch(exception e){ e.printstacktrace(); }
}
}
在myeclipe项目中,要引用mysql的jdbc的jar包: mysql-connector-java-5.0.8-bin.jar , 这个jar包你可以到网上下,或者到mysql官网下.