首先要在数据库中建好表,表的属性要跟代码中的一致
使用jdbk连接数据库,并且进行增删改查的操作(curd).
package com.beiwi; import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle; import java.sql.ResultSet;
import java.sql.Statement; /**
* 数据库的帮助类
* @author Lrs
*
*/
public class DBUtils { private static String driverClass;
private static String url;
private static String user;
private static String password; static{
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
driverClass = rb.getString("driverClass");
url = rb.getString("url");
user = rb.getString("user");
password = rb.getString("password"); try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static Connection getConn() throws Exception{
return DriverManager.getConnection(url, user, password);
} public static void closeAll(ResultSet rs,Statement stmt,Connection conn){ if(null!=rs){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(null!=stmt){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
} if(null!=conn){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} }
package com.beiwi; import java.sql.Connection; import java.sql.ResultSet;
import java.sql.Statement; /**
* 增删改查操作的方法
* create:新增
* refer:查询
* update:更新
* delete:删除
* @author Lrs
*
*/
public class MasterCurd { /**
* 查询所有主人的信息
*/
public void findAll(){
String sql = "SELECT * FROM master";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null; try {
conn = DBUtils.getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql); while (rs.next()) {
System.out.println(rs.getObject(1));
System.out.println(rs.getObject(2));
System.out.println(rs.getObject(3));
System.out.println(rs.getObject(4));
System.out.println("**************************");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBUtils.closeAll(rs, stmt, conn);
} }
/**
* insert一条记录
*/ public void insert(){
String sql = "INSERT INTO master (id,user,password,money) VALUES ('4','haha','456','100');"; Connection conn = null;
Statement stmt = null; try {
conn = DBUtils.getConn();
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils.closeAll(null, stmt, conn);
}
} /**
* 更新一条记录
*/
public void update(){
String sql = "UPDATE master SET user='beiwo1',password='456',money=888 WHERE id=1";
Connection conn = null;
Statement stmt = null; try {
conn = DBUtils.getConn();
stmt = conn.createStatement();
int num = stmt.executeUpdate(sql); if (num>0) {
System.out.println("更新成功");
} } catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils.closeAll(null, stmt, conn);
}
}
/**
* 删除一条记录
*/
public void delete(){
String sql = "DELETE FROM master WHERE id=3";
Connection conn = null;
Statement stmt = null; try {
conn = DBUtils.getConn();
stmt = conn.createStatement();
int num = stmt.executeUpdate(sql); if (num > 0) {
System.out.println("删除成功");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtils.closeAll(null, stmt, conn);
}
}
}
package com.beiwi; import org.junit.Before;
import org.junit.Test; /**
* 测试类
*/ public class TestMasterCrud { MasterCurd curd = null; @Before
public void before(){
curd = new MasterCurd();
}
@Test
public void testFindAll(){
curd.findAll();
}
@Test
public void testInsert(){
curd.insert();
}
@Test
public void testUpdate(){
curd.update();
}
@Test
public void testDelete(){
curd.delete();
}
@Test
public void after(){
curd=null;
} }