java 关于对比数据库表结构前后变化情况。
- 提问者网友:欺烟
- 2021-04-11 04:10
就是要求监测数据库里的表的结构变化
- 五星知识达人网友:我住北渡口
- 2021-04-11 05:15
- 1楼网友:第四晚心情
- 2021-04-11 06:37
- 2楼网友:迷人又混蛋
- 2021-04-11 06:15
数据库的建立应该不用再讲了吧,现在讲使用java与数据库进行交互
使用jdbc进行数据库的增删改查操作
1.下载microsoft sql server 2005 jdbc 驱动包jar文件
将jar文件引入工程中
2.封装数据库链接的获取和关闭操作
import java.sql.*;
public class basedao {
private static final string drive = "com.microsoft.sqlserver.jdbc.sqlserverdriver";
private static final string url = "jdbc:sqlserver://localhost:1433;databasename=bbs";
private static final string user = "sa";
private static final string password = "";
public static connection getconnection() throws exception {
class.forname(drive);
return drivermanager.getconnection(url, user, password);
}
public static void closeall(resultset resultset, preparedstatement pst,
connection connection) throws exception {
if (resultset != null)
resultset.close();
if (pst != null)
pst.close();
if (connection != null)
connection.close();
}
}
3.创建图书的实体类
public class book {
private long id;
private string author;
private string name;
public book() {
}
public book(long id, string author, string name) {
this.id = id;
this.author = author;
this.name = name;
}
public string getauthor() {
return author;
}
public void setauthor(string author) {
this.author = author;
}
public long getid() {
return id;
}
public void setid(long id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
4.创建与图书表交互的工具类
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.util.arraylist;
import java.util.list;
public class bookdao {
public void addbook(book book) throws exception {
// 连接
connection connection = null;
// 执行语句
preparedstatement pst = null;
try {
connection = basedao.getconnection();
// 构造执行语句
string sql = "insert into book values(" + book.getauthor() + ","
+ book.getname() + ")";
pst = connection.preparestatement(sql);
pst.executeupdate();
} catch (exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
basedao.closeall(null, pst, connection);
}
}
public list<book> getbooks() throws exception {
// 用于存放查寻结果的集合
list<book> books = new arraylist<book>();
// 连接
connection connection = null;
// 执行语句
preparedstatement pst = null;
// 查询结果
resultset resultset = null;
try {
connection = basedao.getconnection();
// 构造查询语句
string sql = "select * from book";
pst = connection.preparestatement(sql);
resultset = pst.executequery();
// 循环读取查询结果行
while (resultset.next()) {
// getxxx的参数为数据表列名
book book = new book(resultset.getlong("id"), resultset
.getstring("author"), resultset.getstring("name"));
// 将封装好的图书对象存入集合
books.add(book);
}
} catch (exception e) {
// 抛出异常
throw e;
} finally {
// 无论是否异常 均关闭数据库
basedao.closeall(resultset, pst, connection);
}
// 返回查询结果
return books;
}
}
当然 以上只是简单的封装 初学者可以在理解以上代码的基础上 进行更高级的封装
5.使用bookdao添加书籍和获取所有书籍列表
import java.util.list;
public class test {
public static void main(string[] args) throws exception {
//创建工具类对象
bookdao dao = new bookdao();
//创建一本图书
book book = new book(null,"qq:495691293","编程菜鸟");
//添加书籍到数据库
dao.addbook(book);
//获取所有图书列表
list<book> books = dao.getbooks();
//输出结果
for (book b : books) {
system.out.println(b.getid()+"\t"+b.getauthor()+"\t"+b.getname());
}
}
}
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |