jdbc:mysql://127.0.0.1/student_jdbc-mysql链接-基础版

1、基础连接代码

public static void main(String[] args) throws SQLException {

String url = "jdbc:mysql://127.0.0.1:3306/student";

String userName = "root";

String password = "11111";

String sql = "select id,name,age from user;";

// 1.建立连接

Connection connection = DriverManager.getConnection(url, userName, password);

// 2.具体操作

// 2.1 预编译 SQL

PreparedStatement statement = connection.prepareStatement(sql);

// 2.2 执行查询

ResultSet resultSet = statement.executeQuery();

// 2.3 处理结果集

while (resultSet.next()) {

int id = resultSet.getInt("id");

String name = resultSet.getString("name");

int age = resultSet.getInt("age");

System.out.println("id = " + id + ", name=" + name + ", age=" + age);

}

// 3.资源关闭

resultSet.close();

statement.close();

connection.close();

}

2、基础连接增删改查-加异常处理

static String URL = "jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";

static String USER_NAME = "root";

static String PASSWORD = "123456";

public static void main(String[] args) {

// testInsert1();

testInsert2();

// testUpdate();

// insert , update , delete 操作时只有SQL和数据不一样,其他步骤都一样,所以可以归为一类操作

}

private static void testInsert1() {

String sql = "insert into `tb_user`(`name`,`age`) values('jim','66')";

Connection connection = null;

PreparedStatement statement = null;

try {

// 建立连接

connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

// 创建statement 并执行update

statement = connection.prepareStatement(sql);

int effectRows = statement.executeUpdate();

System.out.println("insert effectRows = " + effectRows);

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

private static void testInsert2() {

String sql = "INSERT INTO `tb_user`(`name`,`age`) VALUES(?,?)";

Connection connection = null;

PreparedStatement statement = null;

try {

// 建立连接

connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

// 创建statement 并执行update

statement = connection.prepareStatement(sql);

statement.setString(1, "王五");

statement.setInt(2, 67);

int effectRows = statement.executeUpdate();

System.out.println("insert effectRows = " + effectRows);

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

private static void testUpdate() {

String sql = "update tb_user set name='haha' where id=6";

Connection connection = null;

PreparedStatement statement = null;

try {

// 建立连接

connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

// 创建statement 并执行update

statement = connection.prepareStatement(sql);

int effectRows = statement.executeUpdate();

System.out.println("update effectRows = " + effectRows);

} catch (SQLException e) {

e.printStackTrace();

} finally {

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

private static void testSelete() {

String sql = "select id ,name ,age from tb_user;";

Connection connection = null;

PreparedStatement statement = null;

ResultSet resultSet = null;

try {

// 1.建立连接

connection = DriverManager.getConnection(url, userName, password);

// 2.具体操作

// 2.1 预编译 SQL

statement = connection.prepareStatement(sql);

// 2.2 执行查询

resultSet = statement.executeQuery();

// 2.3 处理结果集

while (resultSet.next()) {

int id = resultSet.getInt("id");

// 不建议使用

// int id2 = resultSet.getInt(1);

// System.out.println("id2="+id2);

String name = resultSet.getString("name");

int age = resultSet.getInt("age");

System.out.println("id = " + id + ", name=" + name + ", age=" + age);

}

} catch (SQLException e) {

e.printStackTrace();// 不推荐的异常处理方式

} finally {

// 3.资源关闭

if (resultSet != null) {

try {

resultSet.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();

}

}

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值