实现代码如下:(以连接mysql为例,下述程序只进行了插入语句操作 )
第一种方式(非常用)
public static void main(String[] args) {
Statement statement = null;
Connection conn = null;
try {
// 1、注册驱动
Driver driver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(driver);
// 2、获取连接
String url = "jdbc:mysql://127.0.0.1:3306/MyTest";
String user = "root";
String password = "jht123456";
conn = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接对象 = " + conn);
// 3、获取数据库操作对象(statement专门执行sql语句)
statement = conn.createStatement();
// 4、执行sql
String sql = "insert into dept(deptno,dname,loc) values('50','人事','上海')";
int count = statement.executeUpdate(sql);
System.out.println(count == 1 ? "保存成功" : "保存失败");
// 5、处理查询结果集
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 6、释放资源
// 为了保证资源一定释放,在finally语句块中关闭资源
// 并且要遵循从小到大依次关闭
// 分别对其try...catch
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn == null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
相关解释
url:统一资源定位符(网络中某个资源的绝对路径)
包括:协议、IP、PORT(端口)、资源名
以https://www.baidu.com/为例,这就是一个URL
具体表示为:http://182.61.200.7:80/index.html
其中:
http:// 是通信协议
182.61.200.7是服务器IP
80是服务器软件的端口号
index.html是服务器上某个资源名
oracle的URL:
jdbc:oracle:thin:@localhost:1521:orcl
注册驱动的第二种方式 (常用)
// 注册驱动的第二种方式(常用)
// 这是类加载的方式
// Class.forName()会加载类,类加载时,static{}会执行
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
从属性资源文件中读取连接数据库信息
properties文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/MyTest
user=root
password=********
sql=update dept set dname = 'beijing' where deptno = '10'
实现代码
public static void main(String[] args) {
// 使用资源绑定器绑定属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
String sql = bundle.getString("sql");
Connection connection = null;
Statement statement = null;
try {
// 1、注册驱动
Class.forName(driver);
// 2、获取连接
connection = DriverManager.getConnection(url,user,password);
// 3、获取数据库操作对象
statement = connection.createStatement();
// 4、执行sql语句
int count = statement.executeUpdate(sql);
// 5、处理查询结果集
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection !=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
处理查询结果集(遍历结果集)
ResulSet类:结果集类,用于存放查询结果集
实现代码
public static void main(String[] args) {
// 使用资源绑定器绑定属性配置文件
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
String driver = bundle.getString("driver");
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
String sql = bundle.getString("sql");
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
// 1、注册驱动
Class.forName(driver);
// 2、获取连接
connection = DriverManager.getConnection(url,user,password);
// 3、获取数据库操作对象
statement = connection.createStatement();
// 4、执行sql语句
rs = statement.executeQuery(sql); // 专门执行DQL语句的方法
// 5、处理查询结果集
/*boolean flag1 = rs.next();
if(flag1){
// 光标指向的行数据
// 取数据
// getString()方法的特点是:不管数据库中的数据类型是什么,都以String的形式取出,参数是表示行的数
String empno = rs.getString(1);// JDBC所有下标从1开始,不是从0开始
String ename = rs.getString(2);
String sal = rs.getString(3);
System.out.println(empno + "," + ename + "," + sal);
} // 取出一行数据*/
while(rs.next()){
// getString()的参数可以是数字,此时表示第几列的。也可以表示为字符串,此时是查询结果集中的列名。
String empno = rs.getString("empno");// JDBC所有下标从1开始,不是从0开始
String ename = rs.getString("ename");
String sal = rs.getString("sal");
System.out.println(empno + "," + ename + "," + sal);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// 6、释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection !=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}