1.加载数据库驱动程序:各个数据库都会提供JDBC的驱动程序开发包,直接把JDBC操作所需要的开发包(一般为*.jar或*.zip所需的jar包;mysql-connector-java-5.1.21-bin.jar)
直接配置到classpath路径即可。
2.连接数据库:根据各个数据库的不同连接的地址也不同,此连接地址将由数据库厂商提供,一般在使用JDBC连接数据库的时候
都要求用户输入数据库连接的用户名和密码,用户在取得连接之后才可以对数据库进行查询或更新的操作。
3.使用语句进行数据库操作:数据库操作分为更新和查询两种操作,除了可以使用标准的SQL语句之外,对于各个数据库也可以使
直接配置到classpath路径即可。
2.连接数据库:根据各个数据库的不同连接的地址也不同,此连接地址将由数据库厂商提供,一般在使用JDBC连接数据库的时候
都要求用户输入数据库连接的用户名和密码,用户在取得连接之后才可以对数据库进行查询或更新的操作。
3.使用语句进行数据库操作:数据库操作分为更新和查询两种操作,除了可以使用标准的SQL语句之外,对于各个数据库也可以使
用其自己提供的各种命令。
4.关闭数据库连接:数据库操作完毕之后需要关闭连接以释放资源。
package com.pw.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Map;
import com.pw.po.User;
import com.pw.util.DbUtil;
public class Userdao {
// 数据初始化
private String jdbcDriver="com.mysql.jdbc.Driver";
private String jdbcUrl="jdbc:mysql://localhost:3306/pw1";
private String jdbcUser="root";
private String jdbcPassword="123456";
// 添加用户
public boolean saveUser(User user){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
// jdbcDriver驱动加载
Class.forName(jdbcDriver);
// 数据连接
conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
// sql语句控制
String sql="insert into user(username,password,flag) values('"+user.getUsername()+"','"+user.getUsername()+"','"+user.getFlag()+"')";
// 实例化Statement对象
st= conn.createStatement();
// 执行数据库更新的SQL语句,例如:INSERT、UPDATE、DELETE等语句,返回更新的记录数
int row= st.executeUpdate(sql);
System.out.println(row);
if(row>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(st !=null){
try{
st.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn !=null) {
try{
st.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
return flag;
}
// 添加用户
public boolean saveUserDbUtil(User user){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
//
conn=DbUtil.getConnection();
// sql语句控制
String sql="insert into user(username,password,flag) values('"+user.getUsername()+"','"+user.getUsername()+"','"+user.getFlag()+"')";
// 实例化Statement对象
st= conn.createStatement();
// 执行数据库更新的SQL语句,例如:INSERT、UPDATE、DELETE等语句,返回更新的记录数
int row= st.executeUpdate(sql);
System.out.println(row);
if(row>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
}catch(Exception e){
e.printStackTrace();
}
DbUtil.closeAll(st, conn);
return flag;
}
//删除用户
public boolean delUser(int id){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
Class.forName(jdbcDriver);
conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
String sql="delete from user where userid='"+id+"'";
st= conn.createStatement();
int row= st.executeUpdate(sql);
if(row>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
public boolean delUserDBUtil(int id){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
conn=DbUtil.getConnection();
String sql="delete from user where userid='"+id+"'";
st= conn.createStatement();
int row= st.executeUpdate(sql);
if(row>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}catch(Exception e){
e.printStackTrace();
}
DbUtil.closeAll(st, conn);
return flag;
}
// 修改用户
public boolean updateUser(Map<String, Object> map){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
Class.forName(jdbcDriver);
conn=DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
String sql="update user set password='"+(String)map.get("password")+"' where userId="+map.get("id");
st= conn.createStatement();
int row= st.executeUpdate(sql);
System.out.println(row);
}catch(Exception e){
e.printStackTrace();
}finally{
if(st !=null){
try{
st.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(conn !=null) {
try{
st.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
return flag;
}
// 修改用户
public boolean updateUserDbUtil(Map<String, Object> map){
boolean flag =false;
Connection conn=null;
Statement st=null;
try{
conn=DbUtil.getConnection();
String sql="update user set password='"+(String)map.get("password")+"' where userId="+map.get("id");
st= conn.createStatement();
int row= st.executeUpdate(sql);
System.out.println(row);
}catch(Exception e){
e.printStackTrace();
}
DbUtil.closeAll(st, conn);
return flag;
}
// 查找用户
}