Java 数据库操作之Dao类
package Dao; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import bean.Pair;
import util.DBConnection; public class PairDao { public void Addpair(Pair pair) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "insert into pre_info values " + "('" +
pair.getId() +
"','" + pair.getUser() +
"','" + pair.getFollow() + "')";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
//System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
} public List<Pair> QueryUserPair(Pair pair) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "select * from pre_id where user=" + pair.getUser();
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
List<Pair> pairlist = new ArrayList<Pair>();
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Pair pair1 = new Pair();
pair1.setId(rs.getInt(1));
pair1.setUser(rs.getString(2));
pair1.setFollow(rs.getString(3));
pairlist.add(pair1);
System.out.println(pair1);
}
rs.last();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
return pairlist;
} /*
*
*
* public void Addinfo_id(Info info) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "insert into pre_id values " + "('" + info.getId() + "','" + info.getState() + "','" + info.getNowid() + ")";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
}
public void UpdateInfo(Pair info) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "update pre_info set" + " var_id='" + info.getVar_id() + "',var_time='" + info.getVar_time() + "',var_site='" + info.getVar_site()+ "',var_infoid='" + info.getVar_infoid() + "'";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
}
public void DeleteInfo(Pair info) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "delete from pre_info where var_id=" + info.getId();
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
}
public Pair QueryPair(Pair pair) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "select * from pre_id where user=" + pair.getUser();
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
Pair pair1 = new Pair();
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
pair1.setId(rs.getInt(1));
pair1.setUser(rs.getString(2));
pair1.setFollow(rs.getString(3));
}
rs.last();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
return pair1;
}
public Info QueryBook(Info info) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "select * from pre_id where id=" + info.getId();
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
Info info1 = new Info();
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
info1.setId(rs.getString(1));
info1.setState(rs.getString(2));
info1.setNowid(rs.getString(3));
}
rs.last();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
return info1;
}*/ /*public List<Book> QueryBook1(Book book) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "select * from Book where bookname LIKE '%" + book.getBookname() + "%'";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
List<Book> booklist = new ArrayList<Book>();
Book book1 = new Book();
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
book1.setBookname(rs.getString(1));
book1.setBooknumber(rs.getString(2));
book1.setPressname(rs.getString(3));
book1.setAuthor(rs.getString(4));
book1.setTranslator(rs.getString(5));
book1.setPrice(rs.getString(6));
book1.setDate(rs.getString(7));
book1.setBorrowcount(Integer.parseInt(rs.getString(8)));
booklist.add(book1);
// System.out.println(book);
}
rs.last();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
return booklist;
} public List<Book> QueryAllBook() {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "select * from Book order by borrowcount desc";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
List<Book> booklist = new ArrayList<Book>();
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) { Book book = new Book();
book.setBookname(rs.getString(1));
book.setBooknumber(rs.getString(2));
book.setPressname(rs.getString(3));
book.setAuthor(rs.getString(4));
book.setTranslator(rs.getString(5));
book.setPrice(rs.getString(6));
book.setDate(rs.getString(7));
book.setBorrowcount(Integer.parseInt(rs.getString(8)));
booklist.add(book);
System.out.println(book);
}
rs.last();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
return booklist;
} public void UpdateBookBorrowCount(Book book) {
java.sql.Connection connection = DBConnection.getConnection();
String sql = "update Book set " + "borrowcount=" + book.getBorrowcount() + " where booknumber='" + book.getBooknumber() + "'";
java.sql.PreparedStatement pstmt = DBConnection.getPreparedStatement(connection, sql);
System.out.println(sql);
try {
Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
DBConnection.close(connection, pstmt, null);
}
}*/
}
Java 数据库操作之Dao类的更多相关文章
- 复习java数据库操作的总结
以前学习java数据库操作,学得那叫糊里糊涂,各种JDBC常用的类和接口根本是傻傻分不清啥是干嘛的.只是套着用用吧. 不过这次好歹清楚些了,呜呜,学习有阶段性,多次重复才有好效果,多么痛的领悟. 工程 ...
- java web工程 数据库操作报驱动类找不到的错误
这几天在进行数据库的操作,写好数据库操作类后,用测试类测试成功通过,但是部署到tomcat后,从页面访问就会报异常. 最后终于发现是tomcat使用了连接池的数据连接方式. 解决方法是把jdbc ja ...
- Java数据库操作类演示
只在mysql上测试过,不知道算不算好使1. [代码][Java]代码 package org.load.demo; import java.io.IOException;import ja ...
- Java数据库操作学习
JDBC是java和数据库的连接,是一种规范,提供java程序与数据库的连接接口,使用户不用在意具体的数据库.JDBC类型:类型1-JDBC-ODBC桥类型2-本地API驱动类型3-网络协议驱动类型4 ...
- Java数据库操作
一.JDBC 1.JDBC Java数据库连接,用于Java程序中实现数据库操作功能,java.sql包中提供了执行SQL语句,访问各种数据库的方法,并为各种不同的数据库提供统一的操作接口及类. 2. ...
- Java数据库操作(MySQL与SQLserver)
在java编程开发中,数据库的开发是重头戏. MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品: SQL Server是由Microsoft开发 ...
- Java 数据库操作
目录 Java数据库组织架构 下载驱动包 连接数据库 连接数据库的三个步骤 连接数据库的高开销 Statement接口介绍 PreparedStatement类 使用PreparedStatement ...
- Java数据库操作(JDBC)
JDBC Java数据库连接(Java DataBase Connectivity,JDBC)用于在Java程序中实现数据库操作功能,它提供了执行SQL语句.访问各种数据库的方法,并为各种不同的数据库 ...
- Java 学习笔记(16)——Java数据库操作
数据库操作是程序设计中十分重要的一个部分,Java内置JDBC来操作数据库 JDBC使用 JDBC--Java Database connecting Java数据库连接:本质上JDBC定义了操作数据 ...
随机推荐
- JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法
该工程的名称是testhttp,功能是在页面中表格打印浏览过程中的相关头信息. 新建一个工程,然后在这个工程里面新建一个servlet,这样便可以省去编写web.xml的过程 以下是TestHttpS ...
- 后台程序员的HTTP缓存
1.后端程序员只需要关注请求头: if-None-Match //上一次response头中的ETag的值. 响应头: Etag //是URL的Entity Tag,用于标示URL对象是否改变,区分不 ...
- DllMaps
http://www.mono-project.com/docs/advanced/pinvoke/dllmap/ http://www.mono-project.com/docs/advanced/ ...
- C++ Reflection
http://www.vollmann.com/en/pubs/meta/meta/meta.html http://www.extreme.indiana.edu/reflcpp/ http://w ...
- yourphp读取不到hits
源代码 <YP:list name="Article" order="id desc" catid="37" limit=" ...
- [Unity] 2D开发学习教程
豆子先生,据说是官方的一个Demo, 在蛮牛网上有大部分代码的视频讲解. 这个是我学习过程中边看教程边写出来的,功能和原版基本一样,增加了手游的操控. Blog: http://www.cnblogs ...
- yii2.0-advanced 高级版项目搭建
(一) 原文地址:http://www.yii-china.com/post/detail/1.html (二) 原文地址:http://www.yii-china.com/post/detail/2 ...
- python 跨语言数据交互、json、pickle(序列化)、urllib、requests(爬虫模块)、XML。
Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类型]间进 ...
- [Bug]IIs Cannot read configuration file due to insufficient permissions
摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...
- [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
写在前面 前篇文章整理了angularjs学习目录,有园子里的朋友问我要这方面的demo,周末也没什么事,就在之前的单页应用的demo上面添加了增删改查的操作.代码比较简单,这里只列举比较重要的代码片 ...