java基础入门中一把使用JDBC来对关系型数据库进行增删查改操作,首先要搞清楚一个概念,什么是JDBC?JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序.在API里面会发现有几个很核心的类:Connection、PreparedStatement、ResultSet:这几个类具体的API操作请自行在API文档里面研究:
(1)创建Connection对象、SQL查询命令字符串:
(2)对Connection对象传入SQL查询命令,获得PreparedStatement对象;
(3)对PreparedStatement对象执行executeUpdate()或executeQurey()获得结果;
(4)先后关闭PreparedStatement对象和Connection对象。
1.首先建立连接,在建立连接的过程中,需要加载数据库驱动,这里以mysql数据库为例:需要导入mysql的jar包:
mysql jar.png
加载驱动的名字以mysql为例是com.mysql.jdbc.Driver,不同类型的数据库名字也不同:
public static void init() {
try {
//加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动程序");
//连接
connection = DriverManager.getConnection(url);
System.out.println("数据连接成功!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("Connection初始化失败!");
e.printStackTrace();
}
}
2.在操作的过程中那获取PerpareStatement, 使用这个对象去操作数据库,对其进行增删查改操作,具体的代码实现如下:
public class DBConnection {
static String url = "jdbc:mysql://localhost:3306/personInfo?user=root&password=12345678&useUnicode=true&characterEncoding=UTF8";
static Connection connection = null;
static ResultSet resultSet = null;
static PreparedStatement pStatement = null;
// 初始化
public static void init() {
try {
//加载mysql驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功加载MySQL驱动程序");
//连接
connection = DriverManager.getConnection(url);
System.out.println("数据连接成功!");
} catch (Exception e) {
// TODO: handle exception
System.out.println("Connection初始化失败!");
e.printStackTrace();
}
}
// 查询 返回结果集
public static ResultSet qureyDBWithSQL(String sqlStr) {
try {
pStatement = connection.prepareStatement(sqlStr);
resultSet = pStatement.executeQuery(sqlStr);
} catch (Exception e) {
// TODO: handle exception
System.out.println("数据库查询异常!");
e.printStackTrace();
}
return resultSet;
}
// 关闭链接
public static void closeConnection() {
try {
connection.close();
} catch (Exception e) {
// TODO: handle exception
System.out.println("关闭失败!");
e.printStackTrace();
}
}
//更新操作
public static int updteDBWithSql(String sqlStr) {
int rows = 0;
try {
pStatement = connection.prepareStatement(sqlStr);
rows = pStatement.executeUpdate(sqlStr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return rows;
}
//批量插入
public static void mutiplyInsert() {
for (int i= 0; i < 100; i++) {
String sqlstr = "insert into personInfo(personId,name,age)values("+ (100 + i) + ",'Chan" + (100 + i) + "'" + "," + 24 + ")" + ";";
System.out.println(sqlstr);
try {
pStatement = connection.prepareStatement(sqlstr);
pStatement.execute(sqlstr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
操作类的实现代码:
package Dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class OPerationImp implements Operation {
@Override
public List<Person> getAllPerson() {
List<Person> list = new ArrayList<Person>();
try {
DBConnection.init();
//批量插入
DBConnection.mutiplyInsert();
//结果集
ResultSet rSet = DBConnection.qureyDBWithSQL("select * from personInfo");
while (rSet.next()) {
//新建实体类
Person person = new Person();
person.setPersonId(rSet.getInt("personId"));
person.setName(rSet.getString("name"));
person.setAge(rSet.getInt("age"));
list.add(person);
}
DBConnection.closeConnection();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
@Override
public boolean addPerson(Person person) {
// TODO Auto-generated method stub
DBConnection.init();
int rows = 0;
try {
String sqlstr = "insert into personInfo(personId,name,age)values(" + person.getPersonId() + ",'" +person.getName() + "'" +"," + person.getAge() + ");";
rows = DBConnection.updteDBWithSql(sqlstr);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return rows >0;
}
@Override
public boolean deletePerson(int personId) {
// TODO Auto-generated method stub
int rows = 0;
try {
String sqlStr= "delete from personInfo where personId = " + personId;
rows = DBConnection.updteDBWithSql(sqlStr);
} catch (Exception e) {
// TODO: handle exception
}
return rows > 0;
}
@Override
public boolean updatePerson(Person person) {
int rows = 0;
try {
String sqlStr = "update personInfo SET name = '" + person.getName() + "'" + "age = " + person.getAge()
+ "where personId = '" + person.getPersonId() + "'";
rows = DBConnection.updteDBWithSql(sqlStr);
} catch (Exception e) {
// TODO: handle exception
}
return rows > 0;
}
}
main函数实现:
package Dao;
import java.util.List;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
OPerationImp main = new OPerationImp();
List <Person> listArray = main.getAllPerson();
for (int i = 0; i < listArray.size(); i++) {
Person person = (Person)listArray.get(i);
System.out.println("dbQueryResult = " + person);
}
}
}
其实现打印如下:
result print.png
在终端查看数据库如下:
mySQL.png
总结:这里只是简单的对数据库进行了增删查改操作,为了熟悉jdbc的API,写的不好的地方,请私信我指正,如果您觉着对您有帮助的话,给我点一波赞啊!
网友评论