1.jdbc是什么
jdbc是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
通俗的讲,我们需要通过java连接数据库,就需要使用别人写好的java与数据库连接的统一接口,便于我们在不了解java与数据库底层连接机制的情况下,也可以方便的实现数据库与java程序的连接。
2.怎么通过jdbc连接数据库实现增删改
①通过jdbc实现在数据库添加数据的功能
//1.首先将Driver类加载到内存中
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManager类的静态方法获取Connection对象。
Connection con = DriverManager.getConnection(url, name, password);
// 其中url为具体数据库的路径 如:"jdbc:mysql://localhost:3306/user";
// localhost:本机IP地址
// 3306:本机数据库端口号
// user:要连接的数据库名称
// name:要连接的数据库登陆名 如:“root”
// password:要连接的数据库登陆密码 如:“123”
//3.通过Connection对象,创建Statement对象;
Statement stm = con.createStatement();
//4.调用Statement对象的executeUpdate()方法,并把要执行的sql语句,作为参数传进去,返回一个insert记录次数值。
int i = stm.executeUpdate("insert into student values( null, '张三时',‘abc’) where sid = 1");
if(i != 0){ //当返回值i不为0,即可判断插入成功。
System.out.println("插入数据成功!");
}
②通过jdbc实现在数据库删除,修改数据的功能
对比第一点中的添加功能,几乎相似,有一点不同,sql语句需要改成delete… , update…。
3.怎么通过jdbc连接数据库实现查询功能
//1.首先将Driver类加载到内存中
Class.forName("com.mysql.jdbc.Driver");
//2.通过DriverManager类的静态方法获取Connection对象。
Connection con = DriverManager.getConnection(url, name, password);
// 其中url为具体数据库的路径 如:"jdbc:mysql://localhost:3306/user";
// localhost:本机IP地址
// 3306:本机数据库端口号
// user:要连接的数据库名称
// name:要连接的数据库登陆名 如:“root”
// password:要连接的数据库登陆密码 如:“123”
//3.通过Connection对象,创建Statement对象;
Statement stm = con.createStatement();
//4.调用Statement对象的executeQuery()方法,并把要执行的sql语句,作为参数传进去,返回一个ResultSet对象。
ResultSet rs = stm.executeUpdate("insert into student values( null, '张三时',‘abc’) where sid = 1");
while(rs.next()){ //当调用next()方法返回true,即可判断查询到数据了,并且每条数据记录都要调用一次 next()方法。
//如:获取每条记录中的字段为”name"的值。
String str = rs.get(”name“);
System.out.println(str);
}
4.将连接数据库的数据提取到单独文件中
//1.首先定义一个db.properties文件,文件内容如下所示
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test2
username=root
password=123
//2.单独定义一个JDBCUtils类,在里面把获得Connection对象,释放资源代码封装。
public class JDBCUtils {
static String driverClassName = null;
static String url = null;
static String username = null;
static String password = null;
static{
Properties pp = new Properties();
try {
pp.load(new FileInputStream("db.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
driverClassName = pp.getProperty("driverClassName");
url = pp.getProperty("url");
username = pp.getProperty("username");
password = pp.getProperty("password");
}
/**
* 获得连接的方法
*/
public static Connection getConnection(){
Connection con = null;
try {
Class.forName(driverClassName);
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
/**
* 释放资源的方法
*/
public static void release(Statement st, Connection con){
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
st = null;
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
public static void release(ResultSet rs, Statement st, Connection con){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
st = null;
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con = null;
}
}
}