String sql = "select username,password from account"
String user = request.getParameter("user")
String pass = request.getParameter("password")
int j = 0
Connection conn = null
PreparedStatement ps = null
ResultSet rs = null
try {
conn = JDBCTools1.getConnection()
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
//从表中查询获取所有账户的用户名&密码的ResultSet 对象
while(rs.next()){
int i = 0
String username[] = new String[10]//用户名数组
String password[] = new String[10]//密码数组
username[i] = rs.getString(1)
password[i] = rs.getString(2)
if(user.equals(username[i])&&pass.equals(password[i])){//比对
response.getWriter().print("you are welcome!")
j++
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n")
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right")
j++
}else{
continue
}
i++
}
if(j == 0){
response.getWriter().println("Your username may not be properly")
}
} catch (Exception e) {
e.printStackTrace()
}finally{
JDBCTools1.release(rs, ps, conn)
}
//这是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl
//你的数据库的用户名密码
String user = null
String password = null
// 通过反射创建Driver对象
Class.forName(driverClass)
return DriverManager.getConnection(jdbcUrl, user, password)}
//这是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if (statement != null) {
try {
statement.close()
} catch (Exception e2) {
e2.printStackTrace()
}
}
if (conn != null) {
try {
conn.close()
} catch (Exception e2) {
e2.printStackTrace()
}
}
}
使用JDBC进行数据库的增删改查 *** 作1.下载Microsoft SQL Server 2005 JDBC 驱动包jar文件 将jar文件引入工程中2.封装数据库链接的获取和关闭 *** 作import java.sql.*public class BaseDao {\x0d\x0a /**\x0d\x0a * 数据库驱动类的字符串,完整的包名加类名 在工程中查看添加的jar文件 能看到这个类\x0d\x0a */\x0d\x0a private static final String DRIVE = "com.microsoft.sqlserver.jdbc.SQLServerDriver"/**\x0d\x0a * 数据库连接地址\x0d\x0a * \x0d\x0a * DataBaseName=数据库名称 其它固定\x0d\x0a */\x0d\x0a private static final String URL = "jdbc:sqlserver://localhost:1433DataBaseName=bbs"/**\x0d\x0a * 连接数据库的用户名\x0d\x0a */\x0d\x0a private static final String USER = "sa"/**\x0d\x0a * 用户密码\x0d\x0a */\x0d\x0a private static final String PASSWORD = ""/**\x0d\x0a * 获取连接 异常直接抛出 或者捕获后自定义异常信息再抛出\x0d\x0a */\x0d\x0a public static Connection getConnection() throws Exception {\x0d\x0a Class.forName(DRIVE)\x0d\x0a return DriverManager.getConnection(URL, USER, PASSWORD)\x0d\x0a } /**\x0d\x0a * 关闭与数据库的连接 释放资源\x0d\x0a */\x0d\x0a public static void closeAll(ResultSet resultSet, PreparedStatement pst,\x0d\x0a Connection connection) throws Exception {\x0d\x0a if (resultSet != null)\x0d\x0a resultSet.close()\x0d\x0a if (pst != null)\x0d\x0a pst.close()\x0d\x0a if (connection != null)\x0d\x0a connection.close()\x0d\x0a }}3.创建图书的实体类public class Book {\x0d\x0a /**\x0d\x0a * 数据库主键\x0d\x0a */\x0d\x0a private Long id/**\x0d\x0a * 作者\x0d\x0a */\x0d\x0a private String author/**\x0d\x0a * 书名\x0d\x0a */\x0d\x0a private String name\x0d\x0a /**\x0d\x0a * 默认构造\x0d\x0a *\x0d\x0a */\x0d\x0a public Book() {\x0d\x0a }\x0d\x0a /**\x0d\x0a * 全字段构造\x0d\x0a * @param id\x0d\x0a * @param author\x0d\x0a * @param name\x0d\x0a */\x0d\x0a public Book(Long id, String author, String name) {\x0d\x0a this.id = id\x0d\x0a this.author = author\x0d\x0a this.name = name\x0d\x0a }\x0d\x0a /**\x0d\x0a * 以下为读写属性的方法\x0d\x0a * @return\x0d\x0a */\x0d\x0a public String getAuthor() {\x0d\x0a return author\x0d\x0a }\x0d\x0a public void setAuthor(String author) {\x0d\x0a this.author = author\x0d\x0a }\x0d\x0a public Long getId() {\x0d\x0a return id\x0d\x0a }\x0d\x0a public void setId(Long id) {\x0d\x0a this.id = id\x0d\x0a }\x0d\x0a public String getName() {\x0d\x0a return name\x0d\x0a }\x0d\x0a public void setName(String name) {\x0d\x0a this.name = name\x0d\x0a }\x0d\x0a}\x0d\x0a4.创建与图书表交互的工具类import java.sql.Connection\x0d\x0aimport java.sql.PreparedStatement\x0d\x0aimport java.sql.ResultSet\x0d\x0aimport java.util.ArrayList\x0d\x0aimport java.util.Listpublic class BookDao {\x0d\x0a /**\x0d\x0a * 添加新书\x0d\x0a * \x0d\x0a * @param book 要添加入数据库的图书 作者 书名 必须给定\x0d\x0a */\x0d\x0a public void addBook(Book book) throws Exception {\x0d\x0a // 连接\x0d\x0a Connection connection = null\x0d\x0a // 执行语句\x0d\x0a PreparedStatement pst = null\x0d\x0a try {\x0d\x0a connection = BaseDao.getConnection()\x0d\x0a // 构造执行语句\x0d\x0a String sql = "insert into book values(" + book.getAuthor() + ","\x0d\x0a + book.getName() + ")"\x0d\x0a pst = connection.prepareStatement(sql)\x0d\x0a pst.executeUpdate() } catch (Exception e) {\x0d\x0a // 抛出异常\x0d\x0a throw e\x0d\x0a } finally {\x0d\x0a // 无论是否异常 均关闭数据库\x0d\x0a BaseDao.closeAll(null, pst, connection)\x0d\x0a }\x0d\x0a } /**\x0d\x0a * 查询所有书籍列表\x0d\x0a */\x0d\x0a public List getBooks() throws Exception {\x0d\x0a // 用于存放查寻结果的集合\x0d\x0a List books = new ArrayList()\x0d\x0a // 连接\x0d\x0a Connection connection = null\x0d\x0a // 执行语句\x0d\x0a PreparedStatement pst = null\x0d\x0a // 查询结果\x0d\x0a ResultSet resultSet = null\x0d\x0a try {\x0d\x0a connection = BaseDao.getConnection()\x0d\x0a // 构造查询语句\x0d\x0a String sql = "select * from book"\x0d\x0a pst = connection.prepareStatement(sql)\x0d\x0a resultSet = pst.executeQuery() // 循环读取查询结果行\x0d\x0a while (resultSet.next()) {\x0d\x0a// getXXX的参数为数据表列名\x0d\x0aBook book = new Book(resultSet.getLong("id"), resultSet\x0d\x0a .getString("author"), resultSet.getString("name"))\x0d\x0a// 将封装好的图书对象存入集合\x0d\x0abooks.add(book)\x0d\x0a }\x0d\x0a } catch (Exception e) {\x0d\x0a // 抛出异常\x0d\x0a throw e\x0d\x0a } finally {\x0d\x0a // 无论是否异常 均关闭数据库\x0d\x0a BaseDao.closeAll(resultSet, pst, connection)\x0d\x0a }\x0d\x0a // 返回查询结果\x0d\x0a return books\x0d\x0a }/***其它方法类似上面 只是语句不同*/\x0d\x0a}当然 以上只是简单的封装 初学者可以在理解以上代码的基础上 进行更高级的封装\x0d\x0a5.使用BookDao添加书籍和获取所有书籍列表import java.util.List/**\x0d\x0a * 测试类\x0d\x0a * @author Administrator\x0d\x0a *\x0d\x0a */\x0d\x0apublic class Test { /**\x0d\x0a * @param args\x0d\x0a * @throws Exception \x0d\x0a */\x0d\x0a public static void main(String[] args) throws Exception {\x0d\x0a //创建工具类对象\x0d\x0a BookDao dao = new BookDao()\x0d\x0a //创建一本图书\x0d\x0a Book book = new Book(null,"QQ:495691293","编程菜鸟")\x0d\x0a //添加书籍到数据库\x0d\x0a dao.addBook(book)\x0d\x0a \x0d\x0a //获取所有图书列表\x0d\x0a List books = dao.getBooks()\x0d\x0a //输出结果\x0d\x0a for (Book b : books) {\x0d\x0a System.out.println(b.getId()+"\t"+b.getAuthor()+"\t"+b.getName())\x0d\x0a }\x0d\x0a }}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)