java中的基本jdbc中mvc基本示例
数据库:
包文件:
Student.java
1 package com.model;
2
3 public class Student {
4 private int id;
5 private String name;
6 private int age;
7 private String address;
8 public int getId() {
9 return id;
10 }
11 public void setId(int id) {
12 this.id = id;
13 }
14 public String getName() {
15 return name;
16 }
17 public void setName(String name) {
18 this.name = name;
19 }
20 public int getAge() {
21 return age;
22 }
23 public void setAge(int age) {
24 this.age = age;
25 }
26 public String getAddress() {
27 return address;
28 }
29 public void setAddress(String address) {
30 this.address = address;
31 }
32 }
DBUtil.java
1 package com.db;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 public class DBUtil {
10 private static final String URL="jdbc:mysql://127.0.0.1:3306/jdbc";
11 private static final String USER="root";
12 private static final String PASSWORD="gys";
13 private static Connection conn=null;
14 static{
15 try {
16 //加载驱动程序
17 Class.forName("com.mysql.jdbc.Driver");
18 //获取数据库连接
19 conn= DriverManager.getConnection(URL, USER, PASSWORD);
20 } catch (ClassNotFoundException e) {
21 // TODO Auto-generated catch block
22 e.printStackTrace();
23 } catch (SQLException e) {
24 // TODO Auto-generated catch block
25 e.printStackTrace();
26 }
27 }
28
29 public static Connection getConnection(){
30 return conn;
31 }
32
33 public static void main(String[] args) throws Exception{
34
35 //创建statement对象
36 Statement stmt=conn.createStatement();
37 ResultSet rs=stmt.executeQuery("select * from student");
38
39 while(rs.next()){
40 System.out.println(rs.getInt("id")+","+rs.getString("name")+","+rs.getInt("age")+","+rs.getString("address"));
41 }
42
43 }
44 }
StudentDao.java
1 package com.dao;
2
3 import java.sql.Connection;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 import com.db.DBUtil;
13 import com.model.Student;
14
15 /*
16 *
17 */
18 public class StudentDao {
19 // 添加
20 public void addStudent(Student s) throws Exception {
21 Connection conn = DBUtil.getConnection();
22 String sql = "insert into student(name,age,address) values(?,?,?)";
23 PreparedStatement ptmt = conn.prepareStatement(sql);
24 ptmt.setString(1, s.getName());
25 ptmt.setInt(2, s.getAge());
26 ptmt.setString(3, s.getAddress());
27 ptmt.execute();
28 }
29
30 // 更新
31 public void updateStudent(Student s) throws Exception {
32 Connection conn = DBUtil.getConnection();
33 String sql = " update student set name=?,age=?,address=? where id=? ";
34 PreparedStatement ptmt = conn.prepareStatement(sql);
35 ptmt.setString(1, s.getName());
36 ptmt.setInt(2, s.getAge());
37 ptmt.setString(3, s.getAddress());
38 ptmt.setInt(4, s.getId());
39 ptmt.execute();
40 System.out.println("更新完毕.");
41
42 }
43
44 // 删除
45 public void delStudent(Integer id) throws SQLException {
46 Connection conn = DBUtil.getConnection();
47 String sql = " delete from student where id=? ";
48 PreparedStatement ptmt = conn.prepareStatement(sql);
49 ptmt.setInt(1, id);
50 ptmt.execute();
51 System.out.println("删除成功.");
52 }
53
54 // 查询list
55 public List<Student> query() throws Exception {
56 Connection conn = DBUtil.getConnection();
57 Statement stmt = conn.createStatement();
58 ResultSet rs = stmt.executeQuery("select * from student");
59 List<Student> list = new ArrayList<Student>();
60 Student student = null;
61 while (rs.next()) {
62 student = new Student();
63 student.setId(rs.getInt("id"));
64 student.setName(rs.getString("name"));
65 student.setAge(rs.getInt("age"));
66 student.setAddress(rs.getString("address"));
67 list.add(student);
68 }
69 return list;
70 }
71
72 // 查询list
73 public List<Student> query(String name) throws Exception {
74 Connection conn = DBUtil.getConnection();
75 StringBuilder sb = new StringBuilder();
76 sb.append("select * from student ");
77 sb.append(" where name=?");
78 PreparedStatement ptmt = conn.prepareStatement(sb.toString());
79 ptmt.setString(1, name);
80 ResultSet rs = ptmt.executeQuery();
81 List<Student> list = new ArrayList<Student>();
82 Student student = null;
83 while (rs.next()) {
84 student = new Student();
85 student.setId(rs.getInt("id"));
86 student.setName(rs.getString("name"));
87 student.setAge(rs.getInt("age"));
88 student.setAddress(rs.getString("address"));
89 list.add(student);
90 }
91 return list;
92 }
93
94 // 查询list
95 public List<Student> query(List<Map<String, Object>> params)
96 throws Exception {
97 Connection conn = DBUtil.getConnection();
98 StringBuilder sb = new StringBuilder();
99 sb.append("select * from student where 1=1 ");
100 //sb.append(" where name=?");
101 if(params !=null&¶ms.size()>0){
102 for(int i=0;i<params.size();i++){
103 Map<String,Object> map=params.get(i);
104 sb.append(" and "+map.get("name")+" "+map.get("rela")+" "+map.get("value"));
105 }
106 }
107 PreparedStatement ptmt = conn.prepareStatement(sb.toString());
108 //ptmt.setString(1, name);
109 ResultSet rs = ptmt.executeQuery();
110 List<Student> list = new ArrayList<Student>();
111 Student student = null;
112 while (rs.next()) {
113 student = new Student();
114 student.setId(rs.getInt("id"));
115 student.setName(rs.getString("name"));
116 student.setAge(rs.getInt("age"));
117 student.setAddress(rs.getString("address"));
118 list.add(student);
119 }
120 return list;
121 }
122
123 // 查询单个
124 public Student getStudent(Integer id) throws SQLException {
125 Connection conn = DBUtil.getConnection();
126 String sql = " select * from student where id=? ";
127 PreparedStatement ptmt = conn.prepareStatement(sql);
128 ptmt.setInt(1, id);
129 // Statement stmt=conn.createStatement();
130 ResultSet rs = ptmt.executeQuery();
131 Student student = null;
132 while (rs.next()) {
133 student = new Student();
134 student.setId(rs.getInt("id"));
135 student.setName(rs.getString("name"));
136 student.setAge(rs.getInt("age"));
137 student.setAddress(rs.getString("address"));
138 }
139 return student;
140 }
141 }
StudentAction.java
1 package com.action;
2
3 import java.sql.SQLException;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import com.dao.StudentDao;
10 import com.model.Student;
11
12 /*
13 * 控制层
14 */
15 public class StudentAction {
16 public void add(Student student) throws Exception{
17 StudentDao dao=new StudentDao();
18 dao.addStudent(student);
19 }
20 public void edit(Student student) throws Exception{
21 StudentDao dao=new StudentDao();
22 dao.updateStudent(student);
23 }
24 public Student getStudent(Integer id) throws SQLException{
25 StudentDao dao=new StudentDao();
26 return dao.getStudent(id);
27 }
28 public void del(Integer id) throws SQLException{
29 StudentDao dao=new StudentDao();
30 dao.delStudent(id);
31 }
32 public List<Student> query() throws Exception{
33 StudentDao dao=new StudentDao();
34 return dao.query();
35 }
36 public List<Student> query(List<Map<String,Object>> params) throws Exception{
37 StudentDao dao=new StudentDao();
38 return dao.query(params);
39 }
40 //public static void main(String[] args) throws Exception {
41 //StudentDao studentDao=new StudentDao();
42
43 //添加学生
44 /*Student student=new Student();
45 student.setName("嘎嘎");
46 student.setAge(26);
47 student.setAddress("安徽省");
48 studentDao.addStudent(student);*/
49
50 //查询学生list
51 /*List<Student> list=studentDao.query();
52 for(Student s:list){
53 System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
54 }*/
55
56 /*List<Student> list=studentDao.query("嘎嘎");
57 for(Student s:list){
58 System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
59 }*/
60
61 /* List<Map<String,Object>>params=new ArrayList<Map<String,Object>>();
62 Map<String,Object> param=new HashMap<String,Object>();
63 param.put("name","name");
64 //param.put("rela","=");
65 param.put("rela"," like ");
66 param.put("value","'嘎嘎'");
67 params.add(param);
68
69 List<Student> list=studentDao.query(params);
70 for(Student s:list){
71 System.out.println(s.getId()+","+s.getName()+","+s.getAge()+","+s.getAddress());
72 }*/
73 //更新数据
74 /*Student student=new Student();
75 student.setId(3);
76 student.setName("hee");
77 student.setAge(100);
78 student.setAddress("月球上");
79 studentDao.updateStudent(student);*/
80
81 //查询单个学生
82 // Student student=studentDao.getStudent(2);
83 // System.out.println(student.getName()+";"+student.getAge()+","+student.getAddress());
84
85 //删除
86 //studentDao.delStudent(3);
87 //}
88 }
java中的基本jdbc中mvc基本示例的更多相关文章
- 【Java 与数据库】JDBC中日期时间的处理技巧
JDBC中日期时间的处理技巧 详谈Java.util.Date和Java.sql.Date 基础知识 Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒 ...
- java面试题之----jdbc中使用的设计模式(桥接模式)
1.JDBC(JavaDatabase Connectivity) JDBC是以统一方式访问数据库的API. 它提供了独立于平台的数据库访问,也就是说,有了JDBC API,我们就不必为访问Oracl ...
- Java中的事务——JDBC事务和JTA事务
Java中的事务——JDBC事务和JTA事务 转载:http://www.hollischuang.com/archives/1658 之前的事务介绍基本都是数据库层面的事务,本文来介绍一下J2EE中 ...
- 2、Java应用中常见的JDBC连接字符串(SQLite、MySQL、Oracle、Sybase、SQLServer、DB2)
2.Java应用中常见的JDBC连接字符串 Java应用中连接数据库是不可或缺的,于是便整理一些可能用到的JDBC的jar包及其相匹配的URL,以备日后查阅. 1)SQLite Class.forNa ...
- JDBC Java 程序从 MySQL 数据库中读取数据,并备份到 xml 文档中
MySQL 版本:Server version: 5.7.17-log MySQL Community Server (GPL) 相关内容:JDBC Java 程序从 MySQL 数据库中读取数据,并 ...
- Java连接MySQL数据库。编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计。
题目2:编写一个应用程序,在主类Test_4类中,通过JDBC访问stu数据库,显示t_student表中的内容(表结构见表1),显示效果自己设计.之后,可根据显示的内容进行某条记录的删除(以id为条 ...
- JAVA学习7:在Maven仓库中添加Oracle JDBC驱动
由于Oracle授权问题,Maven3不提供Oracle JDBC driver,为了在Maven项目中应用Oracle JDBC driver,必须手动添加到本地仓库. 一.首先要得到Oracle ...
- Java JDBC中,MySQL字段类型到JAVA类型的转换
1. 概述 在使用Java JDBC时,你是否有过这样的疑问:MySQL里的数据类型到底该选择哪种Java类型与之对应?本篇将为你揭开这个答案. 2. 类型映射 java.sql.Types定义了常 ...
- [转]JDBC中日期时间的处理技巧
Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...
随机推荐
- 初探 discuz
测试: vim /etc/hosts ##ip地址转换 修改windows 的配置文件,写字板打开 vim /usr/local/apache/conf/httpd.conf vim /u ...
- JDK1.8源码阅读系列之四:HashMap (原创)
本篇随笔主要描述的是我阅读 HashMap 源码期间的对于 HashMap 的一些实现上的个人理解,用于个人备忘,有不对的地方,请指出- 接下来会从以下几个方面介绍 HashMap 源码相关知识: 1 ...
- 在vi或者vim中显示行号
1. 为了显示行号,在vi/vim编辑器中执行一下命令中的一条 :set number 或者 :set nu 2. 为了关闭行号,在vi/vim编辑器中执行下面命令 :set nu! 如果你想要每次启 ...
- iframe的优缺点?
1.`<iframe>`优点: 解决加载缓慢的第三方内容如图标和广告等的加载问题 Security sandbox 并行加载脚本 2.`<iframe>`的缺点: *ifram ...
- kbhit()的三个测试
#include <stdio.h> #include<conio.h> #include<stdlib.h> int main() { ; system(&quo ...
- 利用AForge.NET 调用电脑摄像头进行拍照
当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...
- android学习1——LinearLayout
用linearLayout,上面放4个按钮,不作任何设置.xml文件内容如下: <?xml version="1.0" encoding="utf-8"? ...
- IntelliJ IDEA 报错:Error:java: 未结束的字符串文字
构建javaweb项目时,控制台报错: 这个问题是由于编码冲突导致的,在设置中找到File Encodings可以看到 ide采用utf-8编码格式,而项目则是GBK编码方式,由此产生冲突. 解决办法 ...
- p1144一元三次方程求解
题目描述: 有形如:f(x)=ax^3+bx^2+cx+d=0这样的一元三次方程,给出该方程中各项的系数a,b,c,d,它们均为实数,并约定该方程一定存在着3个不同的实数解,解的范围在-100至100 ...
- Eclipse通过jdbc连接数据库制作简单登陆界面
一.前言: 做网站开发,要求有多种搭配方式,前台技术可以使用PHP.ASP.JSP.ASP.NET.CGI等任何一种: 需要用到的基础语言用的最多的就是HTML/CSS.JS.JAVA.XML这些了, ...