jdbc-mysql链接-基础版
Posted 黑夜之鹰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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() {
// 使用中文时,有字符编码问题. 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链接-基础版的主要内容,如果未能解决你的问题,请参考以下文章