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);
}
}
}

Java 之jdbc连接mysql数据库的更多相关文章

  1. java用JDBC连接MySQL数据库的详细知识点

    想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...

  2. ava基础MySQL存储过程 Java基础 JDBC连接MySQL数据库

    1.MySQL存储过程   1.1.什么是存储过程 带有逻辑的sql语句:带有流程控制语句(if  while)等等 的sql语句   1.2.存储过程的特点 1)执行效率非常快,存储过程是数据库的服 ...

  3. java 通过jdbc连接MySQL数据库

    先了解下JDBC的常用接口 1.驱动程序接口Driver 每种数据库的驱动程序都应该提供一个实现java.sql.Driver接口的类,简称Driver类.通常情况下,通过java.lang.Clas ...

  4. Java使用JDBC连接MySQL数据库

    1.引用 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...

  5. 【转】Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  6. Java 通过JDBC连接Mysql数据库的方法和实例

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  7. Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  8. JAVA使用JDBC连接MySQL数据库 二

    JAVA连接MySQL稍微繁琐,所以先写一个类用来打开或关闭数据库: public class DBHelper { String driver = "com.mysql.jdbc.Driv ...

  9. Java:jdbc连接mysql数据库

    安装eclipse和mysql的步骤这里不赘述了. 1.一定要下jar包 要想实现连接数据库,要先下载mysql-connector-java-5.1.47(或者其他版本)的jar包.低版本的jar包 ...

  10. 常用JavaBean:JdbcBean codes:Java通过JDBC 连接 Mysql 数据库

    package bean;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class JdbcBean { publi ...

随机推荐

  1. centos 7: 迁移MySQL目录

    Steps: 1. systemctl stop mariadb 2. mkdir data destination folder, ex: /home/mysql 3. cp -R /var/lib ...

  2. Ubuntu 10.04.3 挂载NTFS移动硬盘

    1: fdisk -l #查看所有连接到电脑上的储存设备 2:  mkdir -p /mnt/需要掛的分區 #在mnt裏建立準備掛載分區的目錄 3:  mount -t ntfs /dev/需要掛的分 ...

  3. [JavaEE] Create API documents with Swagger

    We mainly need to modify our Model and REST endpoint code to enable swagger Document. model/Book.jav ...

  4. JWPlayer Uncaught Error: Invalid SRT file

    错误场景: JWPlayer 播放视频,加入了字幕和缩略图: 字幕为Srt格式: 1 00:00:00,000 --> 00:00:02,000 战略管理过程 2 00:00:03,000 -- ...

  5. 模式匹配-BF算法

    /***字符串匹配算法***/ #include<cstring> #include<iostream> using namespace std; #define OK 1 # ...

  6. LeetCode 409. Longest Palindrome (最长回文)

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  7. cocos2d-x之android编译环境搭建(第二篇)[版本号:cocos2d-x-3.1.1]

    基于 Android NDK 的学习之旅-----环境搭建 工欲善其事 必先利其器 , 以下介绍下 Eclipse SDK NDK Cygwin CDT 集成开发环境的搭建. 1.Android 开发 ...

  8. 【JavaScript】JS读取XML文件并进行搜索

    需求效果 点击链接.当前页面载入xml文件并展示相应内容 通过搜索框.搜索xml文件内节点数据.展示包括内容的节点数据 功能实现 Demo终于实现效果 http://loadxmldemo.coder ...

  9. leetCode 60.Permutation Sequence (排列序列) 解题思路和方法

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  10. 逆向工程之App脱壳

    http://www.cnblogs.com/ludashi/p/5725743.html iOS逆向工程之App脱壳 本篇博客以微信为例,给微信脱壳."砸壳"在iOS逆向工程中是 ...