Java连接MySql数据库并实现添加数据
准备阶段:1、环境: java1.8 、mysql5.1.72、工具: eclipse 、 Navicat**3、包结构:**这里要注意导入MySQL资源文件:新建lib——>下载MySQL架包(对应你安装的数据库版本)——>复制粘贴 ——>右击架包——>Build PathOK,开始敲代码实现~~~~1、实体Userpackage com.wmc.jdbcP...
·
准备阶段:
1、环境: java1.8 、mysql5.1.7
2、工具: eclipse 、 Navicat
**3、包结构:**这里要注意导入MySQL资源文件:新建lib——>下载MySQL架包(对应你安装的数据库版本)——>复制粘贴 ——>右击架包——>Build Path
OK,开始敲代码实现~~~~
1、实体User
package com.wmc.jdbcProject.entity;
import java.sql.Date;
public class User {
private int id ;
private String name ;
private String password ;
private Date birthday ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2、配置、连接和关闭数据库资源的工具类DbUtil
package com.wmc.jdbcProject.util;
/**
* 数据库连接的实现
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtil {
private String url = "jdbc:mysql://localhost:3306/db_jdbc_operator?useUnicode=true&characterEncoding=utf8";
private String dbUser = "root" ;
private String dbPassword = "wmc123" ;
private String dbDriver = "org.gjt.mm.mysql.Driver" ;
private Connection connection = null ;
public Connection getConnection() {
try {
Class.forName(dbDriver) ;
connection = DriverManager.getConnection(url,dbUser,dbPassword) ;
System.out.println("数据库连接成功!");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection ;
}
public void closeConnection() {
if(connection !=null) {
try {
connection.close();
System.out.println("数据库连接关闭");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
DbUtil dbUtil = new DbUtil() ;
dbUtil.getConnection();
}
}
3、实现配置、连接和关闭数据库资源的封装类BaseDao:不同的实体都可以通过这个父类实现对数据库的相关操作
package com.wmc.jdbcProject.baseDao;
import java.sql.Connection;
import com.wmc.jdbcProject.util.DbUtil;
public class BaseDao<T> {
private DbUtil dbUtil = new DbUtil() ;
public Connection connection = dbUtil.getConnection();
public void closeConnection() {
dbUtil.closeConnection();
}
}
4、通过继承BaseDao实现Dao类对数据库进行数据添加数据
package com.wmc.jdbcProject.dao;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.wmc.jdbcProject.baseDao.BaseDao;
import com.wmc.jdbcProject.entity.User;
public class UserDao extends BaseDao<User> {
public boolean add(User user) {
String sql= "insert into user(id,name,password,birthday) values(null,?,?,?)" ;
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setDate(3, user.getBirthday());
return preparedStatement.executeUpdate()>0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false ;
}
}
5、逻辑代码实现了,接下来就是新建一个数据库,新建user表的过程:用Navicat工具(连接和使用都很简单!网上都有详细教程,这里就不再赘述!)
6、定义一个测试类Test来验证连接数据库并实现数据添加是否成功?
package com.wmc.jdbcProject.test;
import java.sql.Date;
import com.wmc.jdbcProject.dao.UserDao;
import com.wmc.jdbcProject.entity.User;
public class Test {
public static void main(String[] args) {
User user = new User() ;
user.setName("wmc");
user.setPassword("wmc123");
user.setBirthday(new Date(System.currentTimeMillis()));
UserDao userDao = new UserDao() ;
System.out.println(userDao.add(user));
userDao.closeConnection();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)