JDBC(Java Database Connect)
Java是一种面向对象的语言
||语言不同,需要交流,找个翻译(java只出标准,数据库实现)
Mysql是结构化查询语言
正式连接数据库
java连接数据的步骤
- 把Mysql的jar包拿过来加入到工程下
- 把该jar包解压到本工程下,build path
上面两步在同一个工程下执行一次就行了 - 加载驱动。
Class.forName("com.mysql.jdbc.Driver");
- 获取连接对象。
Connection conn = DriverManager.getConnection(url,user,password);
需要导包:import.java.sql.Connection;
import java.sql.DriverManager;
String url = "jdbc:mysql://IP:3306/数据库名";
String user = "root";
String password = "数据库密码";
- 获取执行sql语句的对象
Statement st = conn.createStatement();
需要导包import java.sql.Statement;
- 执行sql语句。增删改
int row = st.executeUpdate(sql语句);
row返回值表示影响的行数
jdbc:后面加mysql: - 执行sql语句,查询
ResultSet rs = st.executeQuery(sql);
遍历rs对象
while(rs.next()){
xx 变量名 = rs.getxx("字段名"); //获取该记录下指定字段的内容
};
常见的错误
练习:CRUD,使用方法并且关闭资源
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CRUD {
public static void main(String[] args) {
//add("上海");
//delete(4);
//update("南京",4);
selectall();
}
public static void add(String cityname) {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456");
st = conn.createStatement();
int row = st.executeUpdate("insert into city(cityname) values ('"+cityname+"')");
System.out.println("受影响的行数为:"+row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void delete(int cityid) {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456");
st = conn.createStatement();
int row = st.executeUpdate("delete from city where cityID = "+cityid);
System.out.println("受影响的行数为:"+row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void update(String cityname,int cityid) {
Connection conn = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456");
st = conn.createStatement();
int row = st.executeUpdate("update city set cityname = '"+cityname+"' where cityid = "+cityid);
System.out.println("受影响的行数为:"+row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void selectall() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","123456");
st = conn.createStatement();
rs = st.executeQuery("select * from city");
while(rs.next()) {
int cityid = rs.getInt("cityid");
String cityname = rs.getString("cityname");
System.out.println(cityid+"======>"+cityname);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
企业级开发
一张表对应一个操作类(crud),操作类中包含的是CRUD
例如:对emp表操作,那么java中对应的类就是EmpDao(Data Access Object) 这个类尽量放到dao包下
包名:企业域名反向写 www.baidu.com====>com.baidu + 开发人员名称缩写 ====> com.baidu.wlq.dao
练习:抽取一个BaseDao父类(7个属性,一个方法)
写一个员工表的操作类EmpDao 继承Base Dao。包含
- 添加员工(ename,sal,job)
- 删除员工(empNo)
- 修改(ename,sal,job 根据empNo修改)
- 查询 (根据empno查)
- 查询所有员工
并写一个测试类来测试上面5个方法
package com.alibaba.wlq.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BaseDao {
protected Connection conn = null;
protected Statement st = null;
protected ResultSet rs = null;
protected String driver = "com.mysql.jdbc.Driver";
protected String url = "jdbc:mysql://localhost:3306/emp";
protected String user = "root";
protected String password = "123456";
public void closeAll() {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st != null) {
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package com.alibaba.wlq.dao;
import java.sql.DriverManager;
import java.sql.SQLException;
public class EmpDao extends BaseDao{
/**
* 增加员工信息
* @param ename 员工姓名
* @param sal 员工薪水
* @param job 员工职位
*/
public void add(String ename,Double sal,String job) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = <u>conn</u>.createStatement();
String sql = "insert into emp(ename,sal,job,hiredate) values ('"+ename+"',"+sal+",'"+job+"',now())";
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据员工编号删除员工的信息
* @param empno 员工编号
*/
public void delete(int empno) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = conn.createStatement();
String sql = "delete from emp where empno ="+empno;
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 根据员工编号查询员工信息
* @param empno 员工编号
*/
public void select(int empno) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = conn.createStatement();
String sql = "select * from emp where empno = " + empno;
rs = st.executeQuery(sql);
while(rs.next()) {
System.out.println("员工编号:"+rs.getInt("empno"));
System.out.println("员工姓名:"+rs.getString("ename"));
System.out.println("员工薪资:"+rs.getDouble("sal"));
System.out.println("员工入职时间:"+rs.getDate("hiredate"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll();
}
}
/**
* 根据员工编号修改员工姓名、薪资、职位
* @param ename 员工姓名
* @param sal 薪资
* @param job 职位
* @param empno 员工编号
*/
public void update(String ename,Double sal,String job,int empno) {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = conn.createStatement();
String sql = "update emp set ename = '"+ename+"',sal = '"+sal+"',job='"+job+"' where empno = "+empno;
int row = st.executeUpdate(sql);
System.out.println("影响了"+row+"行");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll();
}
}
/**
* 查询所有员工的信息
* @param empno员工编号
*/
public void selectALL() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
st = conn.createStatement();
String sql = "select * from emp";
rs = st.executeQuery(sql);
while(rs.next()) {
System.out.print("员工编号:"+rs.getInt("empno")+" ");
System.out.print("员工姓名:"+rs.getString("ename")+" ");
System.out.print("员工领导编号:"+rs.getInt("mgr")+" ");
System.out.print("员工薪资:"+rs.getDouble("sal")+" ");
System.out.println("员工入职时间:"+rs.getDate("hiredate"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll();
}
}
}
package com.alibaba.wlq.dao;
public class Test1 {
public static void main(String[] args) {
EmpDao emp = new EmpDao();
//emp.add("刘德华",20000.0,"演员");
//emp.delete(1016);
//emp.select(1001);
//emp.update("刘德华", 20000.0, "演员", 1015);
emp.selectALL();
}
}