jdbc封装代码


package jdbcUtil; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import jdbc.RowMap; public class JdbcUtil {
public static Connection getConnection(){
Connection connection=null;
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2创建连接(主机名,端口号,用户名,密码)
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection; } public static void close(Connection connection){
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static int executeUpdate(String sql,Object... params){
int result=0;
Connection connection=getConnection();
PreparedStatement pstmt;
try {
pstmt = connection.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
result=pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(connection);
}
return result;
} public static <T>List<T> executeSelect(String sql,RowMap<T> rowMap,Object...params){
List<T> result=new ArrayList<>();
Connection connection=getConnection();
try {
PreparedStatement pstmt=connection.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
}
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//将数据行 映射到对象中
T t=rowMap.rowMapping(rs);
result.add(t);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(connection);
}
return result;
} }

JdbcUtil.java


package jdbcUtil; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import jdbc.RowMap; public class JdbcUtil {
public static Connection getConnection(){
Connection connection=null;
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2创建连接(主机名,端口号,用户名,密码)
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "123456");
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection; } public static void close(Connection connection){
try {
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static int executeUpdate(String sql,Object... params){
int result=0;
Connection connection=getConnection();
PreparedStatement pstmt;
try {
pstmt = connection.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
result=pstmt.executeUpdate();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(connection);
}
return result;
} public static <T>List<T> executeSelect(String sql,RowMap<T> rowMap,Object...params){
List<T> result=new ArrayList<>();
Connection connection=getConnection();
try {
PreparedStatement pstmt=connection.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pstmt.setObject(i+1, params[i]);
}
}
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//将数据行 映射到对象中
T t=rowMap.rowMapping(rs);
result.add(t);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
close(connection);
}
return result;
} }

jdbcBean.java


package jdbc; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;import jdbcUtil.JdbcUtil;class jdbcBean {
public static void main(String[] args) {
List<Student> list=select();
System.out.println(list);
/*jdbcBean jdbcBean=new jdbcBean();
jdbcBean.tset();*/
/*delete();*/
/*add();*/
} public static int delete(){
return JdbcUtil.executeUpdate("delete from student where sid=? and sname=?", 10,"¹þÊ¿Ææ"); } public static int update(){
return JdbcUtil.executeUpdate("update student set sname=?,age=?,sex=? where sid=?","mojie",2,"ÐÛ",1); } public static int add(){
return JdbcUtil.executeUpdate("insert into student(sname,age,sex) values(?,?,?)", "ÍÛ¹þ",3,"ÐÛ"); }
public static List select(){ return jdbcUtil.JdbcUtil.executeSelect("select * from student",new RowMap<Student>(){
@Override
public Student rowMapping(ResultSet rs){
Student student=new Student();
try {
student.setAge(rs.getInt("age"));
student.setName(rs.getString("sname"));
student.setSex(rs.getString("sex"));
student.setSid(rs.getInt("sid"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
}, null);
/*Connection connection=JdbcUtil.getConnection();
List<Student> list=new ArrayList<>();
try {
PreparedStatement pstmt=connection.prepareStatement("select * from student");
ResultSet rs= pstmt.executeQuery();
while (rs.next()) {
Student student=new Student();
student.setAge(rs.getInt("age"));
student.setName(rs.getString("sname"));
student.setSex(rs.getString("sex"));
student.setSid(rs.getInt("sid"));
list.add(student); }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JdbcUtil.close(connection);
}
return list;*/
}
public void tset(){
HashMap<String,Student> sHashMap=new HashMap<>();
for(int i=0;i<4;i++){
System.out.println("qingshuru");
Scanner scanner=new Scanner(System.in);
Integer sid=scanner.nextInt();
String sname=scanner.next();
Integer age=scanner.nextInt();
String sex=scanner.next();
Student student=new Student(sname, age, sid, sex);
sHashMap.put("Student"+i, student);
}
for(int i=0;i<4;i++){
System.out.println(sHashMap.get("Student"+i));
} }
}

RowMap.java


package jdbc; import java.sql.ResultSet; public interface RowMap<T> {
public T rowMapping(ResultSet rs);
}

Student.java


package jdbc; public class Student {
private String name;
private int age;
private int sid;
private String sex;
public String getName() {
return name;
}
public Student(String name, int age, int sid, String sex) {
super();
this.name = name;
this.age = age;
this.sid = sid;
this.sex = sex;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
public Student(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public Student() {
super();
// TODO Auto-generated constructor stub
} }

jdbc封装代码的更多相关文章

  1. 优化JDBC封装

    可重用性较强的JDBC封装 以下为代码,注释中写了主要思想 主类 com.util.JDBCUtil.java package com.util; import java.lang.reflect.F ...

  2. 七、Block 封装代码

    1.概念:封装代码块,调用的时候使用 2.声明 返回类型(^名字)(参数1,参数2..) = (参数类型 变量1,参数类型, 变量2){ }; int (^Sum)(int,int)  = ^(int ...

  3. [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码

    函数是一种将代码作为数据结构存储的便利方式,代码之后可以被执行.这使得富有表现力的高阶函数抽象如map和forEach成为可能.它也是js异步I/O方法的核心.与此同时,也可以将代码表示为字符串的形式 ...

  4. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  5. python网页请求urllib2模块简单封装代码

    这篇文章主要分享一个python网页请求模块urllib2模块的简单封装代码. 原文转自:http://www.jbxue.com/article/16585.html 对python网页请求模块ur ...

  6. <<海闻电子发票接口 ESB 封装 代码指示 文档>>

    <<海闻电子发票接口 ESB 封装 代码指示 文档>> isValid 是否有效标志 代码 中文 说明 true 成功 false 失败   code 海闻错误说明 代码 中文 ...

  7. Ajax--json(Ajax调用返回json封装代码、格式及注意事项)

    Ajax调用json封装代码<dbda.php>: //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb ...

  8. 完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络]

    完整java开发中JDBC连接数据库代码和步骤[申明:来源于网络] 地址:http://blog.csdn.net/qq_35101189/article/details/53729720?ref=m ...

  9. Uiautomator ---(1) 封装代码

    http://www.cnblogs.com/by-dream/p/4996000.html  上面是别人的写法 我自己的写法: package qq.test; import android.con ...

随机推荐

  1. Pat(Advanced Level)Practice--1026(Table Tennis)

    Pat1026代码 题目描写叙述: A table tennis club has N tables available to the public. The tables are numbered ...

  2. 监视EF生成SQL的方法(log , SqlServerProfile)

    大家在学习entityframework的时候,都知道那linq写的叫一个爽,再也不用区分不同RDMS的sql版本差异了,但是呢,高效率带来了差灵活性,我们 无法控制sql的生成策略,所以必须不要让自 ...

  3. spring-redis SortedSet类型成员的过期时间处理

    redis默认是只支持简单key的过期处理的,像SortedSet类型,也是针对整个set的过期处理,不支持对set的某个成员的过期处理: 为了解决这个问题,做法如下: 1.存储key及值信息到red ...

  4. Database Designer

    DBDesigner http://fabforce.net/dbdesigner4/index.php DB Designer Fork http://sourceforge.net/project ...

  5. MVC 发布程序 HTTP 错误 403.14 - Forbidden 及 HTTP 错误 404.2 - Not Found

    新建立的MVC项目发布程序后会浏览网站可能会有问题 这时不要去按照系统提示打开“目录浏览”,而是应该去做一些配置 具体步骤: 1:配置web.Config <system.webServer&g ...

  6. vue+node+mongoDB 火车票H5(二)---vux和less的配置

    vue基本环境配置好之后,就可以开始开发页面了 开发页面之前先了解一下项目结构,原始的目录结构为: config是配置文件,环境配置好了开发阶段可以不再去修改了,node_modules文件夹是打包的 ...

  7. Canvas-图片填充-预加载

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. IDEA 设置代码模板

    一.代码模板 参考: IntelliJ IDEA 使用(一)基本设置与类.方法模板设置 - 云 + 社区 - 腾讯云 文件代码模板的使用 - IntelliJ IDEA 使用教程 - 极客学院 Wik ...

  9. 基于minikube的kubernetes集群部署及Vitess最佳实践

    简介 minikube是一个可以很容易在本地运行Kubernetes集群的工具, minikube在电脑上的虚拟机内运行单节点Kubernetes集群,可以很方便的供Kubernetes日常开发使用: ...

  10. 第5章 IDA Pro实验题

    Question: 1.DLLMain的地址是什么? 2.使用import窗口并浏览到gethostbyname,导入函数定位到什么位置 3.有多少函数调用了gethostbyname? 4.将精力放 ...