根据极客网视频整理出来的资料,和自己的理解。供自己和大家以后学复习习时候看一看。
1.JDBC介绍(通用,规范的语句)
1.一种执行SQL语言的Java API
2.可以对主流数据库进行统一的访问(Access,MySQL,SQLserver,Oracle)
3.极大的减少了程序操作数据库的复杂性
4.与ODBC的区别,需要先调用C语言的代码,稳定性效率低,
5.JDBC 实用面向对象的方式的存储过程
6.JDBC可以直接调用数据库的存储过程
7.JDBC的操作箱效率高
2.连接数据库的步骤
1.创建项目
2.导入MySQL连接jar包到项目中
3.利用导入的包完成连接数据库的工具类、
MySQL jar类的下载地址:
http://dev.mysql.com/downloads/connector/j/
public class BaseConnection {
//Connection对象来进行数据库的连接对象
public static Connection getConnection(){
//声明类
Connection conn = null;
try {
//加载驱动类,驱动类的包
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接 连接路径,用户名,密码
conn = DriverManager.
getConnection("jdbc:mysql://localhost:3306/newsmanager","root","1163534699");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//测试连接数据库方法
public static void main(String[] args) {
Connection conn = BaseConnection.getConnection();
System.out.println(conn);
}
}
当运行后如果出现
com.mysql.jdbc.JDBC4Connection@377dca04
类似这样的字符就表明连接成功
类包的具体分别
包名 命名规范
bean实体类包 数据库表名的GetSet方法
dao操作数据库的方法包
main实际的操作方法
util工具类包
进行修改查询,修改添加的时候要先创建 实体的类别类 就是你数据库表的具体类
这是我的数据库表
News类
public class News {
private int id;
private String title;
private String content;
private int type;
private String name;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
NewsType类
public class NewsType {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.查询语句
3.1单表查询
/**
* 新闻的查询列表 单表查询
*/
public ArrayList<News> getList() {
// 创建一个新的ar,用来存储到获取的内容
ArrayList<News> ar = new ArrayList<News>();
// 获取一条数据库连接
Connection conn = BaseConnection.getConnection();
// 声明一个PreparedStatement对象(SQL执行器对象)
// PreparedStatement提高安全性,效率
PreparedStatement ps = null;
// 声明一个结果集对象(查询的内容首先放在结果集的对象),存放的是读取的一行数据
// 存在指针的,从第0行开始读取
ResultSet rs = null;
try {
String sql = "select * from news ";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
// 将rs的指针进行下移,如果有数据,则返回true值,进行读取
while (rs.next()) {
// 创建一个实体类对象,将数据封装到实体类中(每查询一列,封装一个对象)
News ne = new News();
ne.setId(rs.getInt("id"));
ne.setTitle(rs.getString("title"));
ne.setContent(rs.getString("content"));
ne.setType(rs.getShort("type"));
ar.add(ne);
}
} catch (Exception e) {
e.printStackTrace();
}finally { //关闭
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return ar;
}
}
3.2多表查询
public ArrayList<News> getListAll() {
// 创建一个新的ar,用来存储到获取的内容
ArrayList<News> ar = new ArrayList<News>();
// 获取一条数据库连接
Connection conn = BaseConnection.getConnection();
// 声明一个PreparedStatement对象(SQL执行器对象)
// PreparedStatement提高安全性,效率
PreparedStatement ps = null;
// 声明一个结果集对象(查询的内容首先放在结果集的对象),存放的是读取的一行数据
// 存在指针的,从第0行开始读取
ResultSet rs = null;
try {
String sql = "select * from news,newstype where news.type = newstype.id";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
// 将rs的指针进行下移,如果有数据,则返回true值,进行读取
while (rs.next()) {
// 创建一个实体类对象,将数据封装到实体类中(每查询一列,封装一个对象)
News ne = new News();
ne.setId(rs.getInt("id"));
ne.setTitle(rs.getString("title"));
ne.setContent(rs.getString("content"));
ne.setType(rs.getInt("type"));
ne.setName(rs.getString("name"));
ar.add(ne);
}
} catch (Exception e) {
e.printStackTrace();
// 把打开的数据库关闭掉,如果不关闭,则一直进行连接
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return ar;
}
4.添加数据
4.1添加数据的方法(使用占位符)
// 1.占位符方式效率较高
// 2.拼写时,不容易出错
// 3.防止SQL注入,调高安全性
public void insert2(News ne) {
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "insert into news(title,content,type) " + "values(?,?,?)"; // 问号表示占位符
System.out.println(sql);
try {
// prepareStatement起到传递员的作用,负责将sql语句传递到数据库
ps = conn.prepareStatement(sql);
// 对占位符的数据进行封装,根据数据类型不同调用setString
ps.setString(1, ne.getTitle());
ps.setString(2, ne.getContent());
ps.setInt(3, ne.getType());
// executeUpdate用于改变数据库数据 ,a表示你改变了数据库的条数
int a = ps.executeUpdate();
if (a > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭数据库
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
4.2添加数据的方法(SQL语句的拼接)
public void insert(News ne) {
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "insert into news(title,content,type) " + "values('" + ne.getTitle() + "','" + ne.getContent()
+ "','" + ne.getType() + "')"; // 添加数据字符串型的要加单引号
System.out.println(sql);
try {
// prepareStatement起到传递员的作用,负责将sql语句传递到数据库
ps = conn.prepareStatement(sql);
// executeUpdate用于改变数据库数据 ,a表示你改变了数据库的条数
int a = ps.executeUpdate();
if (a > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭数据库
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
5.修改语句
/**
* 修改数据库操作 本方法用于将传递过来的news对象中的值,根据id主键,改变数据库的值
*/
public void update(News ne) {// 要传入参数(数据库类的对象)
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "update news set title = ? ,content = ?,type=? where id =?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ne.getTitle());
ps.setString(2, ne.getContent());
ps.setInt(3, ne.getType());
ps.setInt(4, ne.getId());
int a = ps.executeUpdate();
if (a > 0) {
System.out.println("修改成功");
} else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {// 关闭数据库
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
6.删除方法
/**
* 删除的方法
*/
public void delete(int id) {
Connection conn = BaseConnection.getConnection();
PreparedStatement ps = null;
String sql = "delete from news where id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
int a = ps.executeUpdate();
if(a >0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
}finally {// 关闭数据库
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
主方法
public static void main(String[] args) {
//删除
NewsTypeDAO de = new NewsTypeDAO();// 把工具类进行实例化
de.delete(4);
// // 修改
// NewsTypeDAO alter = new NewsTypeDAO();// 把工具类进行实例化
// News ne = new News(); // 把
// ne.setTitle("活着真累");
// ne.setContent("累也要活着");
// ne.setType(2);
// ne.setId(4);
// alter.update(ne);
// 添加
// NewsTypeDAO add = new NewsTypeDAO();// 把工具类进行实例化
// News ne = new News(); // 把
// ne.setTitle("北京一市民坚持晨练30年");
// ne.setContent("最终得了肺癌");
// ne.setType(2);
// add.insert2(ne);// 添加语句
// // 查询方法
// ArrayList<News> ar = new NewsTypeDAO().getListAll();
// for (News ne : ar) {
// System.out.println(ne.getId() + "标题:" + ne.getTitle() + ",内容:" +
// ne.getContent() + ne.getName());
// }
}
把一个方法和一个主方法放在一个类中就可以运行的
一个测试的类
public class TestMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
NewsDAOoptimization nd = new NewsDAOoptimization();
while (true) {
System.out.println("1.查看新闻 2.添加新闻 3.删除新闻 4.退出");
int a = sc.nextInt();
if (a == 1) {
ArrayList<News> ar = nd.getListAll();
System.out.println("编号\t标题\t内容");
for (News ne : ar) {
System.out.println(ne.getId() + "\t" + ne.getTitle() + "\t" + ne.getContent() + "\t");
}
} else if (a == 2) {
System.out.println("请输入新闻标题,内容,类别,编号");
News ne = new News();
ne.setTitle(sc.next());// 将用户输入的值封装到news对象中
ne.setContent(sc.next());
ne.setType(sc.nextInt());
boolean b = nd.insert2(ne);// 插入到数据库
System.out.println(b);
} else if (a == 3) {
System.out.println("请输入要删除的新闻编号");
int id = sc.nextInt();
boolean b = nd.delete(id);
System.out.println(b);
} else {
break;
}
}
}
}
代码优化(把可以替换掉的重复的语句放在一个方法里面进行封装调用)