package connection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;public class BasicJDBC {public static void main(String[] args) {Connection connection = null;Statement statement = null;try {// 建立与数据库的Connection连接// 这里需要提供:// 数据库所处于的ip:127.0.0.1 (本机)// 数据库的端口号: 3306 (mysql专用端口号)// 数据库名称 sakila// 编码方式 UTF-8// 账号 root// 密码 rootconnection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/sakila?characterEncoding=UTF-8", "root", "root");statement = connection.createStatement();// 准备sql语句String sql = "SELECT * FROM actor";statement.execute(sql);System.out.println("获取 Statement对象: " + statement);} catch (SQLException e) {throw new RuntimeException(e);} finally {// 数据库的连接时有限资源,相关操作结束后,养成关闭数据库的好习惯// 先关闭Statementif (statement != null)try {statement.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 后关闭Connectionif (connection != null)try {connection.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。