目录
idea导入jar包
- 创建lib文件夹,把jar包拷贝到该目录
- 右键选择add to library
第一个jdbc程序
1、加载驱动
2、连接数据库DriverManager
3、获得执行SQL的对象 Statement
4、获得返回的结果集
5、释放连接
import java.sql.*;
// 我的第一个jdbc程序
public class jdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//加载驱动
Class.forName("com.mysql.jdbc.Driver"); //固定写法 加载驱动
//用户信息和URL
String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username="root";
String password="123456";
//连接成功,数据库对象 Connection代表数据库
Connection connection = DriverManager.getConnection(url, username, password);
//执行SQL的对象 statement执行SQL的对象
Statement statement=connection.createStatement();
//执行SQL的对象去执行SQL,可能存在返回结果,查看返回结果
String sql="SELECT * FROM users";
ResultSet resultSet = statement.executeQuery(sql); // 返回的结果集
while(resultSet.next()){
System.out.println("id="+resultSet.getObject("id"));
System.out.println("name="+resultSet.getObject("NAME")); //字段要和数据库的字段一致
System.out.println("pwd="+resultSet.getObject("PASSWORD"));
System.out.println("email="+resultSet.getObject("email"));
System.out.println("birth="+resultSet.getObject("birthday"));
System.out.println("=====================================");
}
//释放连接
resultSet.close();
statement.close();
connection.close();
}
}
DriverManager
Class.forName("com.mysql.jdbc.Driver"); //固定写法 加载驱动 Connection connection = DriverManager.getConnection(url, username, password);connection代表数据库
数据库设置自动提交
数据提交
数据回滚
connection.setAotoCommit();
connection.commit();
connection.rollback();
URL
String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";mysql 端口号是3306
协议://主机地址:端口号/数据库名?参数1&参数2&参数3
oralce 端口号是1521
jdbc:oralce:thin:@localhost:1521:sid
Statement 执行SQL的类 PrepareStatement
String sql="SELECT * FROM users"; //编写SQL statement.executeQuery(); //查询操作返回 ResultSet statement.execute(); //执行任何SQL statement.executeUpdate(); //更新,插入,删除都是用这个,返回一个受影响的行数
ResultSet查询的结果集:封装了所有的查询结果
resultSet.getObject(); //在不知道列类型的情况下使用 //如果知道列的类型就使用指定的类型 resultSet.getString(); resultSet.getInt(); resultSet.getFloat(); resultSet.getDate();
遍历,指针
resultSet.beforeFirst(); //移动到最前面 resultSet.afterLast(); //移动到最后面 resultSet.next(); //移动到下一个数据 resultSet.previous(); //移动到前一行 resultSet.absolute(row); //移动到指定行
释放资源 这些连接很占空间
resultSet.close(); statement.close(); connection.close();
statement对象
jdbc中的statement对象用于向数据库发送SQL语句,向完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查的语句即可。
但使用statement对象不太安全,会存在SQL注入的问题。
SQL注入
是指web应用程序对用户输入数据的合法性没有判断或过滤不严,攻击者可以在web应用程序中事先定义好的查询语句的结尾上添加额外的SQL语句,在管理员不知情的情况下实现非法操作,以此来实现欺骗数据库服务器执行非授权的任意查询,从而进一步得到相应的数据信息。
关键是因为在写SQL的时候存在一些拼接操作,比如使用'' or '1=1'替代,它总是成立,因而不正确输入用户名也能得到数据库的那些数据。
jdbc工具类
public class JbdcUtils {
private static String driver=null;
private static String url=null;
private static String username=null;
private static String password=null;
static {
try{
InputStream in = JbdcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties=new Properties();
properties.load(in);
driver=properties.getProperty("driver");
url=properties.getProperty("url");
username=properties.getProperty("username");
password=properties.getProperty("password");
//驱动只加载一次
Class.forName(driver);
}catch (Exception e){
e.printStackTrace();
}
}
// 获取连接
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
// 释放资源
public static void release(Connection conn, Statement st, ResultSet rs){
if(rs!=null){
try {
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
增删改查测试代码
一般只需要改变SQL语言即可
插入
public class TestInsert {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection(); //获取数据库连接
st=conn.createStatement(); //获得SQL的执行对象
String sql="INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)" +
"VALUES(4,'小罗','123456','456872@qq.com','2020-01-01')";
int i = st.executeUpdate(sql);
if(i>0){
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
删除
public class TestDelete {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection(); //获取数据库连接
st=conn.createStatement(); //获得SQL的执行对象
String sql="DELETE FROM users WHERE id=4";
int i = st.executeUpdate(sql);
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
更新
public class TestUpdate {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection(); //获取数据库连接
st=conn.createStatement(); //获得SQL的执行对象
String sql="UPDATE users SET `NAME`='小罗',`email`='2209993661@qq.com' WHERE id=2";
int i = st.executeUpdate(sql);
if(i>0){
System.out.println("更新成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
查询
public class TestSelect {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=JdbcUtils.getConnection();
st=conn.createStatement();
String sql="select * from users where id=1";
rs=st.executeQuery(sql);
while (rs.next()){
System.out.println(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
PreparedStatement对象
PreparedStatement对象可以防止SQL注入,效率更好。
插入
public class TestInsert {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement st=null;
try {
conn=JdbcUtils.getConnection();
//使用?占位符代替参数
String sql="INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) values(?,?,?,?,?)";
st=conn.prepareStatement(sql); //预编译SQL,先写SQL,但是不执行
//手动给参数赋值
st.setInt(1,4);
st.setString(2,"luo");
st.setString(3,"124568");
st.setString(4,"45687923@qq.com");
//sql.Date() 数据库使用的时间
//util.Date() JAVA使用的时间 new Date().getTime() 获得时间戳
st.setDate(5,new java.sql.Date(new Date().getTime()));
int i = st.executeUpdate();
if(i>0){
System.out.println("插入成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,null);
}
}
}
删除
public class TestDelete {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement st=null;
try {
conn=JdbcUtils.getConnection();
//使用?占位符代替参数
String sql="delete from users where id=?";
st=conn.prepareStatement(sql); //预编译SQL,先写SQL,但是不执行
//手动给参数赋值
st.setInt(1,4);
int i = st.executeUpdate();
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,null);
}
}
}
更新
public class TestUpdate {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement st=null;
try {
conn=JdbcUtils.getConnection();
//使用?占位符代替参数
String sql="update users set `NAME`=? where id=?";
st=conn.prepareStatement(sql); //预编译SQL,先写SQL,但是不执行
//手动给参数赋值
st.setString(1,"小刘");
st.setInt(2,2);
int i = st.executeUpdate();
if(i>0){
System.out.println("更新成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,null);
}
}
}
查询
public class TestSelect {
public static void main(String[] args) {
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
try {
conn=JdbcUtils.getConnection();
String sql="select * from users where id=?";
st=conn.prepareStatement(sql);
st.setInt(1,1);
rs=st.executeQuery();
if(rs.next()){
System.out.println(rs.getString("NAME"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}
IDEA连接数据库
需要导入connect的包