jdbc getgeneratedkeys mysql_Java 之jdbc连接mysql数据库

package jdbc;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

public class JdbcUtils

{

private static String url;

private static String user;

private static String password;

static {

try {

//FileInputStream in = new FileInputStream(new File("./src/db.properties"));

InputStream in = JdbcUtils.class.getResourceAsStream("/db.properties");

Properties properties = new Properties();

properties.load(in);

Class.forName(properties.getProperty("driver"));

url = properties.getProperty("url");

user = properties.getProperty("user");

password = properties.getProperty("password");

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

/**

* 获取连接

* @return

*/

public static Connection getConnection() {

try {

return DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

/**

* 关闭连接

*/

public static void close(Statement stateMent, Connection connection) {

if (stateMent != null) {

try {

stateMent.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

}

}

}

}

package jdbc.statement;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import jdbc.JdbcUtils;

import org.junit.Test;

public class StateMentTest {

@Test

public void testInsert() {

Connection conn = null;

Statement stmt = null;

try {

conn = JdbcUtils.getConnection();

stmt = conn.createStatement();

String sql = "insert into test (name) values ('test')";

int count = stmt.executeUpdate(sql);

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

JdbcUtils.close(stmt, conn);

}

}

@Test

public void testUpdate() {

Connection conn = null;

Statement stmt = null;

try {

conn = JdbcUtils.getConnection();

stmt = conn.createStatement();

String sql = "update test set name = 'update' where id=4";

int count = stmt.executeUpdate(sql);

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

JdbcUtils.close(stmt, conn);

}

}

@Test

public void testDelete() {

Connection conn = null;

Statement stmt = null;

try {

conn = JdbcUtils.getConnection();

stmt = conn.createStatement();

String sql = "delete from test where id=4";

int count = stmt.executeUpdate(sql);

System.out.println(count);

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

JdbcUtils.close(stmt, conn);

}

}

@Test

public void testSelect() {

Connection conn = null;

Statement stmt = null;

try {

conn = JdbcUtils.getConnection();

stmt = conn.createStatement();

String sql = "select * from test";

ResultSet resultSet = stmt.executeQuery(sql);

while(resultSet.next()) {

System.out.println(resultSet.getString("name"));

}

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally {

JdbcUtils.close(stmt, conn);

}

}

}

package jdbc.prepared;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import jdbc.JdbcUtils;

import org.junit.Test;

public class PreparedStateMent {

@Test

public void testInsert()

{

Connection conn = JdbcUtils.getConnection();

String sql = "insert into test (name) values (?)";

PreparedStatement pstmt = null;

try {

pstmt = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);

pstmt.setString(1, "prepared");

int count = pstmt.executeUpdate();

ResultSet resultSet = pstmt.getGeneratedKeys();

if (resultSet.next()) {

System.out.println(resultSet.getString(1));

}

System.out.println(count);

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally{

JdbcUtils.close(pstmt, conn);

}

}

@Test

public void testUpdate()

{

Connection conn = JdbcUtils.getConnection();

String sql = "update test set name = ? where id = ?";

PreparedStatement pstmt = null;

try {

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, "prepareds");

pstmt.setInt(2, 6);

int count = pstmt.executeUpdate();

System.out.println(count);

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally{

JdbcUtils.close(pstmt, conn);

}

}

@Test

public void testDelete()

{

Connection conn = JdbcUtils.getConnection();

String sql = "delete from test where id = ?";

PreparedStatement pstmt = null;

try {

pstmt = conn.prepareStatement(sql);

pstmt.setInt(1, 6);

int count = pstmt.executeUpdate();

System.out.println(count);

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally{

JdbcUtils.close(pstmt, conn);

}

}

@Test

public void testSelect()

{

Connection conn = JdbcUtils.getConnection();

String sql = "select * from test";

PreparedStatement pstmt = null;

try {

pstmt = conn.prepareStatement(sql);

ResultSet set = pstmt.executeQuery();

while(set.next()) {

System.out.println(set.getString("name"));

}

} catch (SQLException e) {

e.printStackTrace();

throw new RuntimeException(e);

} finally{

JdbcUtils.close(pstmt, conn);

}

}

}

标签:jdbc,Java,mysql,stmt,JdbcUtils,sql,import,conn,pstmt

来源: https://www.cnblogs.com/yang-2018/p/10356431.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值