jdbc遵循以下四个步骤,接下来会详细记录每部的操作:
1/加载驱动 2/创建连接 3/创建并执行sql语句 4/关闭资源
在第三部会有两种操作:分别是Statement 和PreparedStatement 两种形式的sql语句传输,后者是前者的子类 ,后者继承前者,同时进行了改进,后者比前者更加安全可靠灵活
package com.xsh.testJDBC;
import org.apache.log4j.Logger;
import java.sql.*;
/**
* 纯java方式连接数据库
* Created by Administrator on 2017/12/25.
*/
public class JdbcDemo {
/***
* 1. 加载驱动
* 2. 建立连接
* 3. 执行语句
* 4. 关闭连接
*/
static Logger logger = Logger.getLogger(JdbcDemo.class);
public static void main(String[] args) {
String name = "王五";
int age= 20;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//加载驱动
System.out.println("加载驱动...");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
logger.error(e);
e.printStackTrace();
}
//创建连接 jdbc:mysql://localhost:3306/smbms
try {
//本Demo操作的是本地MySQL数据库153jdbc数据库的stu表
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/153jdbc","root","mysql");
statement = connection.createStatement();
//创建sql语句.
/*
// 增加--------
StringBuffer sql = new StringBuffer("INSERT INTO stu(name,age) value ('");
sql.append(name+"','");
sql.append(age+"');");
// 注意 本句使用的execute方法返回值是布尔类型,表示返回的是否是一个ResultSet数据集
statement.execute(sql.toString());*/
//更新
String sql = "update stu set name = '刘德华',age=15 where id = 3;";
//executeUpdate(sql) 查看源代码可以发现 该方法返回值是int类型 表示影响的行数 可以用一个int数据类型接收
int ss=statement.executeUpdate(sql);
System.out.println(ss);
/* //查询
String sql = "select * from stu";
//获取结果集 ,得到一个对象数组
resultSet=statement.executeQuery(sql);
while (resultSet.next()){
//可以在getXxx() 的括号里些下表或者直接写对象属性 但下标注意是从 -1- 开始的
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getInt("age"));
}*/
} catch (SQLException e) {
logger.error(e);
System.out.println("连接失败");
e.printStackTrace();
}
//关闭连接
try {
if (statement!=null){
statement.close();
System.out.println("statement关闭成功...");
}
if (connection!=null){
connection.close();
System.out.println("关闭成功...");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
PreparedStatement 解析
package com.xsh.testJDBC;
import org.apache.log4j.Logger;
import java.sql.*;
/**
* 纯java方式连接数据库
* Created by Administrator on 2017/12/25.
*/
public class JdbcDemo2 {
/***
* 1. 加载驱动
* 2. 建立连接
* 3. 执行语句
* 4. 关闭连接
*/
static Logger logger = Logger.getLogger(JdbcDemo2.class);
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//加载驱动
System.out.println("加载驱动....");
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
logger.error(e);
e.printStackTrace();
}
//创建连接 jdbc:mysql://localhost:3306/smbms
try {
//本Demo操作的是本地MySQL数据库153jdbc数据库的stu表
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/153jdbc","root","mysql");
//创建SQL语句,使用preparedStatement 对象属性后不跟数据 而是通过占位符占位 后续补充
String sql ="UPDATE stu SET name=? ,age = ? WHERE id = ?";
preparedStatement =connection.prepareStatement(sql);
preparedStatement.setString(1,"dada");
preparedStatement.setInt(2,88);
preparedStatement.setInt(3,3);
//上述语句已经完成对sql语句的输送,所以下句的executeUpdate方法里是无参的
preparedStatement.executeUpdate();
System.out.println("更新成功...");
} catch (SQLException e) {
logger.error(e);
System.out.println("连接失败");
e.printStackTrace();
}
//关闭连接
try {
if (preparedStatement!=null){
preparedStatement.close();
System.out.println("pstm 关闭成功...");
}
if (connection!=null){
connection.close();
System.out.println("conn 关闭成功...");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}