既然JDBC主要是用于java连接数据库的,能连接什么数据库没有指定,其实能连接很多种数据库,而且一般来说可以连接oracle和mysql,通常也是这两种。但是既然JDBC能连接这么多的数据库,开发起来太麻烦了,于是sun公司那帮人想出了一个办法,我定义一套规则,大家都按照这个规则来,实现自己公司访问数据库的实现。这套规则就是JDBC,遵循了JDBC规范的,可以访问自己数据库的API被称之为驱动。
驱动:我们的jdbc的驱动就是那个jar
真正干活的是不是就是我们的这个驱动--jdbc就是控制我们的驱动进行干活
它怎么控制我们的驱动进行干活,这就是我们要学习的东西三个接口一个类
DriverManager :依据数据库的不同,管理JDBC驱动 Connection :负责连接数据库并担任传送数据的任务 Statement :由 Connection 产生、负责执行SQL语句 ResultSet:负责保存Statement执行后所产生的查询结果
jdbc工作模板
try {
2 Class.forName(JDBC驱动类);
3 }
4 … …
5 try {
6 Connection con=DriverManager.getConnection(URL,数据库用户名,密码);
7
8 Statement stmt = con.createStatement();
9 ResultSet rs = stmt.executeQuery("SELECt a, b, c FROM Table1");
10
11 while (rs.next()) {
12 int x = rs.getInt("a");
13 String s = rs.getString("b");
14 float f = rs.getFloat("c");
15 }
16 rs.close();
17 stmt.close();
18 con.close();
案例1:使用纯java方式连接数据库
public class TestStamp01 {
public static void main(String[] args) {
//Connection 用来连接数据库,并传送数据
Connection connection = null;
Statement st = null;
//1、加载驱动
try {
Class.forName("com.mysql.jdbc.Driver"); //mysql驱动加载的固定写法
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2、建立连接
//连接的是数据库,url:告诉驱动在那个数据库中干活 用户名,数据库密码
//创建一个连接的url
String url = "jdbc:mysql://localhost:3306/test";
try {
connection =DriverManager.getConnection(url,"root", "root123456");
System.out.println("连接成功");
//connection 用于连接数据库并且传送数据
//3、执行sql接口(statement)他是由 connection接口产生的,
//statement 用来执行我们的sql
//st= connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(connection!=null) { //关闭连接
try {
connection.close();
System.out.println("连接关闭");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
使用Statement进行增删改
-- 新增的语法 insert into (添加的字段) 表名 values(值) insert into subject (subName,teachid) values ('html','20014') -- 主键在新增的时候需不需要进行设值,当我们的主键被设置自动增长了就不需要,没有设置,你在添加的时候一定要给主键唯一值
insert into subject (subName,teachid) values ('html','20014')
public class TestStamp02 {
public static void main(String[] args) {
Connection connection = null;
Statement st = null;
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//建立连接
// 创建连接的url:告诉jdbc连接的是那个数据库
String url ="jdbc:mysql://localhost:3306/test";
//进行连接
try {
connection = DriverManager.getConnection(url,"root","root123456");
//执行SQL执行SQL的方法全部在statement接口中
st = connection.createStatement();
String sql = "insert into subject (subName,techid) VALUES('jsp','0123')";
String squlupdate ="update subject set subName ='html'where subid = 16";
String sqlDelete = "delete from subject where subid = 15";
//调用Statement对象中的新增,修改,删除的方法
int a = st.executeUpdate(sqlDelete); //executeUpdate 可以执行新增,修改,删除 返回执行条数
System.out.println("删除成功");
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭资源
try {
if(connection!=null) {
connection.close();
System.out.println("资源已经关闭");
}else if(st!=null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
使用Statement和ResultSet进行查询
public class TestStamp03 {
public static void main(String[] args) {
//Connection 用来连接数据库,并传送数据
Connection connection = null;
Statement st = null;
ResultSet rs = null;
//1、加载驱动
try {
Class.forName("com.mysql.jdbc.Driver"); //mysql驱动加载的固定写法
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2、建立连接
//连接的是数据库,url:告诉驱动在那个数据库中干活 用户名,数据库密码
//创建一个连接的url
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
try {
connection =DriverManager.getConnection(url,"root", "root123456");
System.out.println("连接成功");
//connection 用于连接数据库并且传送数据
//3、执行sql接口(statement)他是由 connection接口产生的,
//statement 用来执行我们的sql
st= connection.createStatement();
//执行sql
String sql = "select subId,subName,teachid from subject";
//查询是为了得到结果,不是为了得到查询的条数,查询出来的结果放到了ResultSet
rs = st.executeQuery(sql); //executeQuery执行的是查询
System.out.println("查询成功");
System.out.println("tt成绩列表");
System.out.println("成绩编号t 课程名称t 老师编号t");
//从ResultSet将结果取出来
while(rs.next()) { //next 去判断ResultSet是否还有结果
//System.out.print(rs.getInt(1)+"t");
//System.out.print(rs.getString(2)+"t");
//System.out.print(rs.getInt(3)+"n");
//拿去数据第二种 根据列名去拿
System.out.print(rs.getInt("subId")+"t");
System.out.print(rs.getString("subName")+"t");
System.out.print(rs.getString("teachid")+"n");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(connection!=null) {
connection.close();
}else if(st!=null) {
st.close();
}else if(rs!=null) {
rs.close();
}
System.out.println("连接关闭");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
preparedstatement和statement的区别
1.PreparedStatement是预编译的,对于批量处理可以大大提高效率.也叫JDBC存储过程 2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得,preparedstatement支持批处理
使用preparedstatement和ResultSet进行查询public class TestStamp04 {
private static Logger logget = Logger.getLogger(TestStamp01.class);
public static void main(String[] args) {
Connection connect = null;
Statement st = null; //Statement有执行新增,修改,删除,查询的方法
PreparedStatement pst = null; //Statement有执行新增,修改,删除,查询的方法
ResultSet rs = null;
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
logget.error(e);
}
//创建连接 url
String url = "jdbc:mysql://localhost:3306/test";
//进行连接
try {
connect= DriverManager.getConnection(url,"root","root123456");
logget.info("连接成功");
//创建statement对象
// st =connect.createStatement();
//编写sql语句
String sql03 ="select * from subject where teachid=? and subName = ?";
pst = connect.prepareStatement(sql03);
pst.setInt(1, 1);
pst.setString(2, "java基础");
rs =pst.executeQuery(); //executeQuery执行查询方法
logget.info("添加成功");
while(rs.next()) {
System.out.print(rs.getInt("subId")+"t");
System.out.print(rs.getString("subName")+"t");
System.out.print(rs.getInt(3));
}
} catch (SQLException e) {
logget.error(e);
}finally {//关闭资源
try {
if(connect!=null) {
connect.close();
}else if(st!=null) {
st.close();
}else if(rs!=null) {
rs.close();
}else if(pst!=null) {
pst.close();
}
} catch (SQLException e) {
logget.error(e);
}
}
}
}
执行新增
public class TestStamp04 {
private static Logger logger = Logger.getLogger(TestStamp02.class);
public static void main(String[] args) {
Connection connection = null;
//Statement st = null; //获取sql执行sql方法
PreparedStatement ps = null;
ResultSet rs = null ;//获取返回结果集
try {
Class.forName("com.mysql.jdbc.Driver"); //加载驱动
} catch (ClassNotFoundException e) {
logger.error(e);
}
//建立连接
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
try {
connection = DriverManager.getConnection(url, "root", "root123456");
System.out.println("数据库连接成功");
// 创建Statement对象,此对象中有修改删除的方法
// st = connection.createStatement();
//声明执行的sql语句
String sql = "select * from subject where subID = ? and subName=?";
String sql01 ="insert into subject(subNme,subID) values(?,?)";
//rs=st.executeQuery(sql);
ps = connection.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2,"java基础");
ps.executeUpdate();
rs = ps.executeQuery(); //执行查询语句
logger.info("查询成功");
System.out.println("tt 课程信息列表");
System.out.println("课程编号t 课程名称t 老师idt");
while(rs.next()) {
System.out.print(rs.getInt(1)+"t");
System.out.print(rs.getString(2)+"t");
System.out.print(rs.getInt(3)+"t");
}
} catch (SQLException e) {
logger.error(e);
}finally {
try {
if(ps !=null){
ps.close();
} else if(connection!=null) {
connection.close();
}else if(rs!=null) {
rs.close();
}
}catch (SQLException e) {
logger.error(e);
}
}
}
}


