菁英科技(卓目鸟学苑)- 专注软件测试菁英教育
标题:
java_赵梦冰_2021-03-15
[打印本页]
作者:
Loading...
时间:
2021-3-15 19:30
标题:
java_赵梦冰_2021-03-15
public class
SqliteDb {
//
连
接
数
据
库
private
Connection
connect
() {
// jdbc:
数
据
库类
型,
数
据
库
相
对应
于工程路
径
String url =
"jdbc:sqlite:src/equ/db/equ.db"
;
try
{
//
加
载类
Class.
forName
(
"org.sqlite.JDBC"
)
;
//
连
接
数
据
库
Connection conn = DriverManager.
getConnection
(url)
;
System.
out
.println(
"
连
接
数
据
库
成功
"
)
;
return
conn
;
}
catch
(ClassNotFoundException | SQLException e) {
System.
out
.println(
"
连
接
数
据
库异
常,
异
常信息
为
:
"
+ e.getMessage())
;
}
return null;
}
//
断开数
据
库连
接
private void
disconnect
(Connection conn){
try
{
conn.close()
;
}
catch
(SQLException e){
System.
out
.println(
"
断开数
据
库异
常,
异
常信息
为
:
"
+ e.getMessage())
;
}
}
//
执
行
sql
语
句,
执
行
插
入、
删
除、更新
类
的操作,返回
true/false
private boolean
executeUpdate
(Connection conn
,
String sql){
// Statement
是
执
行
sql
语
句的接口
Statement statement =
null;
try
{
statement = conn.createStatement()
;
int
count = statement.executeUpdate(sql)
;
System.
out
.println(
"
执
行
sql
语
句成功,影
响
"
+ count +
"
条数
据
"
)
;
statement.close()
;
return
count >=
1
;
}
catch
(SQLException e) {
System.
out
.println(
"
执
行
sql
语
句
异
常,
异
常信息:
"
+ e.getMessage())
;
}
return false;
}
//
执
行
sql
语
句,
执
行
查询类
的操作,返回
查询结
果
private
List<Equ>
executeQueery
(Connection conn
,
String sql){
List<Equ> equs =
new
ArrayList<>()
;
Statement statement =
null;
try
{
statement = conn.createStatement()
;
ResultSet rs = statement.executeQuery(sql)
;
if
(rs !=
null
){
//
查询结
果不
为
空
while
(rs.next()){
//
根据列表
获
取每一列的
值
String id = rs.getString(
"id"
)
;
String name = rs.getString(
"name"
)
;
String location = rs.getString(
"location"
)
;
String baoFei = rs.getString(
"baoFei"
)
;
String buyTime = rs.getString(
"buyTime"
)
;
String baoFeiTime = rs.getString(
"baoFeiTime"
)
;
String record = rs.getString(
"record"
)
;
//
根据
获
取的信息
构
造
Equ
对
象
Equ equ =
new
Equ(id
,
name
,
location
,
baoFei
,
buyTime
,
baoFeiTime
,
record)
;
equs.add(equ)
;
}
System.
out
.println(
"
执
行
sql
语
句,
查询
到
"
+ equs.size() +
"
条数
据
"
)
;
}
}
catch
(SQLException e){
System.
out
.println(
"
执
行
sql
语
句
异
常,
异
常信息
为
:
"
+ e.getMessage())
;
}
return
equs
;
}
欢迎光临 菁英科技(卓目鸟学苑)- 专注软件测试菁英教育 (http://www.zmnxy.com/)
Powered by Discuz! X3.4