记得导连接数据的依赖包
jdbc依赖包下载地址
不多说,直接附上代码
package com.fang.jdbc;
import java.sql.*;
/**
* @program: blog
* @description: 连接数据库并进行简单的操作
* @author: Fang
* @create: 2020-05-11 11:14
**/
public class JDBC {
/**
* 取得与数据库的连接
*
* @return Connection
*/
public Connection getConnection() {
//数据库账号
String user = "root";
//数据库密码
String pwd = "666";
// 定义连接接口
Connection con = null;
try {
/*------oracle数据库----*/
//加载驱动器
Class.forName("oracle.jdbc.driver.OracleDriver");
//连接协议
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
/*---------mysql--------*/
//加载驱动器
// Class.forName("com.mysql.jdbc.Driver");
//连接协议
//String url="jdbc:mysql://localhost:3306/mysql里面你所创建的数据库的名字";
//如果是mysql高于8.0版本包括8.0:com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver
//且url改为jdbc:mysql://localhost:3306/mysql里面你所创建的数据库的名字?useSSL=false&serverTimezone=UTC
con = DriverManager.getConnection(url, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 添加数据
*
* @return
*/
public boolean insert() {
//取得连接
Connection connection = getConnection();
//创建Sql语句预执行器--防止恶意的sql注入
PreparedStatement ps = null;
String sql = "insert into tb_test (name,age,sex) values(?,?,?)";
boolean flag = false;
try {
//预编译
ps = connection.prepareStatement(sql);
ps.setString(1, "fang");
ps.setString(2, "666");
ps.setString(3, "男");
//开始执行
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
}
/**
* 删除
*
* @return
*/
public boolean delete(String name) {
//取得连接
Connection connection = getConnection();
//创建Sql语句预执行器--防止恶意的sql注入
PreparedStatement ps = null;
String sql = "delete from tb_test where name=?";
boolean flag = false;
try {
//预编译
ps = connection.prepareStatement(sql);
ps.setString(1, name);
//开始执行
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
}
/**
* 修改数据
*
* @return
*/
public boolean update(String name, int age) {
//取得连接
Connection connection = getConnection();
//创建Sql语句预执行器--防止恶意的sql注入
PreparedStatement ps = null;
String sql = "update tb_test set age=? where name=?";
boolean flag = false;
try {
//预编译
ps = connection.prepareStatement(sql);
ps.setInt(1, age);
ps.setString(2, name);
//开始执行
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return flag;
}
/**
* 根据名字查询数据
*
* @return
*/
public void query(String name) {
//取得连接
Connection connection = getConnection();
//创建Sql语句预执行器--防止恶意的sql注入
PreparedStatement ps = null;
String sql = "select * from tb_test where name=?";
ResultSet rs = null;
try {
//预编译
ps = connection.prepareStatement(sql);
ps.setString(1, name);
//开始执行
rs = ps.executeQuery();
while (rs.next()) {
/*通过数据库表中的字段名取数据*/
String uname = rs.getString("name");
int age = rs.getInt("age");
String sex = rs.getString("sex");
System.out.println("name="+ uname);
System.out.println("age="+ age);
System.out.println("sex="+ sex);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}