码迷,mamicode.com
首页 > 数据库 > 详细

jdbc-mysql链接-基础版

时间:2020-05-27 20:42:42      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:finally   nts   query   man   password   数据   insert   time   exec   

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() {
// 使用中文时,有字符编码问题. TODO : 字符集编码问题需要设置一下
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();
}
}
}
}

jdbc-mysql链接-基础版

标签:finally   nts   query   man   password   数据   insert   time   exec   

原文地址:https://www.cnblogs.com/duanjialin007/p/12976033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!