一、作为一名测试人员工作中经常接触到一些数据库的内容。经常使用到的无非是mysql、oracle、Sqlserver,今天干好有时间整理一下使用java实现数据库的基本操作。
1、独立新建class,封装方法返回conn
public class MysqlConn {
/**
*
* @param DBDRIVER 类名加载驱动
* @param DBURL 数据库地址
* @param DBUSER 数据库用户
* @param DBPASS 用户密码
* @return 数据库连接串
*/
public Connection DBConnection_get(String DBDRIVER,String DBURL,String DBUSER,String DBPASS) {
Connection conn=null;
try {
Class.forName(DBDRIVER); //通过类名加载数据库驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); //获取数据库连接
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
}
return conn; //调用本方法获取到返回结果需做判断是否为空
}
}
2、建立要操作的数据库的表所对应的class(其实也可以不需要,但是为了方便我还是提前建好了这个类)
public class tableclass {
private String id;
private String name;
private String age;
private String sex;
private String classes;
private String gread;
private String phonenumber;
//以及个属性的 get和set方法、tostring方法(不做详细说明)
}
3、建立实现数据库操作的类,完成增删改查方法
public class MysqlOperation {
private java.sql.Statement stmt = null;
private ResultSet rs=null;
private Connection conn=null;
/**
*
* @param conn 传入数据库连接构造对象
*/
public MysqlOperation(Connection conn){
this.conn=conn;
try {
this.stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param Sql 待执行的sql语句
* @return 查询结果 ResultSet
*/
public ResultSet query(String Sql){
try {
rs = stmt.executeQuery(Sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
*
* @param Sql 增删改语句
*/
public void add_del_update(String Sql)
{
java.sql.PreparedStatement psmt = null;
try {
psmt = conn.prepareStatement(Sql); // 预编译语句
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
psmt.execute(); //执行语句
System.out.println(Sql+":执行成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
4、测试
public class Tooltest {
public static void main(String[] args) {
final String DBDRIVER = "com.mysql.jdbc.Driver"; //驱动
final String DBURL = "jdbc:mysql://localhost/yfzz"; //数据库地址
final String DBUSER = "root"; //用户名
final String DBPASS = "yfb.love.1314"; //密码
Connection conn = null; //连接串
try {
Class.forName(DBDRIVER); //加载驱动
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); //获取数据库连接
if (conn!=null){
System.out.println(conn); //输出连接串信息
MysqlOperation MO=new MysqlOperation(conn); //创建数据库操作类对象
String QueSql="SELECT * from studentinfo";
String InsSql1="INSERT INTO studentinfo(idnumber,name,age,sex,classes,gread,phonenumber)VALUES('10000','张三','18','男','高三','5班','13888886666')";
String InsSql2="INSERT INTO studentinfo(idnumber,name,age,sex,classes,gread,phonenumber)VALUES('10001','李四','18','男','高三','5班','13888886666')";
String UpdSql="UPDATE studentinfo SET phonenumber='13912345678'";
String DelSql="DELETE from studentinfo where NAME='张三'";
MO.add_del_update(InsSql1);
MO.add_del_update(InsSql2);
MO.query(QueSql);
ResultSet rs=MO.query(QueSql);
if(rs!=null)
{
while(rs.next()){
tableclass tb=new tableclass();
tb.setId(rs.getString(1));
tb.setName(rs.getString(2));
tb.setAge(rs.getString(3));
tb.setSex(rs.getString(4));
tb.setClasses(rs.getString(5));
tb.setGread(rs.getString(6));
tb.setPhonenumber(rs.getString(7));
tb.toString();
}
}
MO.add_del_update(UpdSql);
MO.add_del_update(DelSql);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //通过类名加载数据库驱动
}
}