- 在项目的根目录>创建Folder(文件夹)命名lib,然后导入jar文件.
- 右击jar文件>Build Path(构建路径)>Add to Build Path,此时Referenced Libraries(引用库)下就有了jar包.
- 点击Fefereced LIbraries>找到com.mysql.jdbc(包)>Driver.class右击Copy Qualified Name(复制限定名)全路径com.mysql.jdbc.Driver
- 在src(文件夹)>创建File(文件)命名database.properties(配置文件)
name | value |
driver | com.mysql.jdbc.Driver |
url | jdbc:mysql://localhost:3306/myschool(数据库) |
userName | root(用户名) |
password | 123(密码) |
/**
*连接数据类
/
/*配置文件database.properties
driver=com.mysql.jdbc.Driver;
url=jdbc:mysql://localhost:3306/数据库名
userName="root";
password="123";*/
public class BaseDao {
public static String driver;
public static String url;
public static String username;
public static String password;
public static Connection conn;
public static PreparedStatement prepareStatement;
public static ResultSet resultSet;
/*
*读取配置文件(静态代码块只执行一次)
*/
static {
//获取配置文件的全路径
String path="database.properties";
//将配置文件加到到输入流中
InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(path);
//创建读取配置文件的对象properties
Properties properties=new Properties();
try {
//加载输入流
properties.load(is);
} catch (Exception e) {
e.printStackTrace();
}
//通过key取value值
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
}
/*
*返回Connection对象
*/
public static Connection getConn(){
try {
//反射加载当前数据库的驱动
Class.forName(driver);
//通过驱动管理器获得connection对象
conn=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/*
*查询的方法(返回ResultSet结果集)
*/
public static ResultSet select(String sql, Object... objects) {
conn=getConn();
try {
prepareStatement=conn.prepareStatement(sql);
if (objects!=null) {
for (int i = 0; i < objects.length; i++) {
prepareStatement.setObject(i+1, objects[i]);
}
}
resultSet=prepareStatement.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
return resultSet;
}
/*
*增删改的方法(返回false表示成功)
*/
public static boolean update(String sql, Object... objects) {
conn = getConn();
boolean f = true;
try {
prepareStatement = conn.prepareStatement(sql);
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
prepareStatement.setObject(i + 1, objects[i]);
}
}
f = prepareStatement.execute();
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
/*
*增删改的方法(返回受影响的行数)
*/
public static int update(String sql, Object... objects) {
conn = getConn();
int count=0;
try {
prepareStatement = conn.prepareStatement(sql);
if (objects != null) {
for (int i = 0; i < objects.length; i++) {
prepareStatement.setObject(i + 1, objects[i]);
}
}
count = prepareStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/*
*关闭连接(先开后关的原则)
*/
public void clo() {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (prepareStatement != null) {
try {
prepareStatement.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
-