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);
}
}
}
Java 之jdbc连接mysql数据库的更多相关文章
- java用JDBC连接MySQL数据库的详细知识点
想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...
- ava基础MySQL存储过程 Java基础 JDBC连接MySQL数据库
1.MySQL存储过程 1.1.什么是存储过程 带有逻辑的sql语句:带有流程控制语句(if while)等等 的sql语句 1.2.存储过程的特点 1)执行效率非常快,存储过程是数据库的服 ...
- java 通过jdbc连接MySQL数据库
先了解下JDBC的常用接口 1.驱动程序接口Driver 每种数据库的驱动程序都应该提供一个实现java.sql.Driver接口的类,简称Driver类.通常情况下,通过java.lang.Clas ...
- Java使用JDBC连接MySQL数据库
1.引用 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写 ...
- 【转】Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- Java 通过JDBC连接Mysql数据库的方法和实例
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- JAVA使用JDBC连接MySQL数据库 二
JAVA连接MySQL稍微繁琐,所以先写一个类用来打开或关闭数据库: public class DBHelper { String driver = "com.mysql.jdbc.Driv ...
- Java:jdbc连接mysql数据库
安装eclipse和mysql的步骤这里不赘述了. 1.一定要下jar包 要想实现连接数据库,要先下载mysql-connector-java-5.1.47(或者其他版本)的jar包.低版本的jar包 ...
- 常用JavaBean:JdbcBean codes:Java通过JDBC 连接 Mysql 数据库
package bean;import java.sql.*;import com.mysql.jdbc.PreparedStatement;public class JdbcBean { publi ...
随机推荐
- C/C++ Threads): Creating worker threads that will be listening to jobs and executing them concurrently when wanted
Suppose we have two workers. Each worker has an id of 0 and 1. Also suppose that we have jobs arrivi ...
- python基础学习之02 元组
#encoding=utf-8 # 元组与列表一样,也是一种序列 print (1,2,3) print 1,2,3 print(1,) print 1, a=1 print(type(a)) a=1 ...
- Effective C++ Item 44 将与參数无关的代码抽离 templates
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:Templates 生成多个 classes 和多个函数,所以不论什么 templat ...
- 解决Mysql存储中文的问题
Mysql无法存储中文或者中文乱码,当然是编码的问题.你可以mysql -u root -p进入Mysql命令行环境,然后输入命令查看当前编码格式: mysql> show variables ...
- unity3d-23种设计模式全解析
http://www.jianshu.com/nb/4161593 2016.08.03 09:26 字数 1203 阅读 584评论 0喜欢 14 希望大家能共同学习,交流 谢谢支持zero(QQ: ...
- 【EasyUi DataGrid】批量删除
DataGrid是我们做网页经常使用到的组件之中的一个,对它的操作也无非是增删改查操作.单条数据的增删改相对来说比較简单.添加.改动能够直接在DataGrid中进行,也能够用弹出框的形式把数据装载在文 ...
- network adapter
1 network adapter 网络适配器,也叫网卡,是一个硬件. 2 关于以太网卡 以太网卡分为光纤以太网卡和电口以太网卡. 3 常用的以太网卡 3.1 AMD PCNet PCI II (Am ...
- sql_server_action
''' SELECT * FROM Info_Roles WHERE Flag=1 LIMIT 2; select top y * from 表 where 主键 not in(select top ...
- 从基于 SQL 的 CURD 操作转移到基于语义 Web 的 CURD 操作
中文名称 CURD 含义 数据库技术中的缩写词 操作对象 一般的项目开发的各种参数 作用 用于处理数据的基本原子操作 它代表创建(Create).更新(Update).读取(Retrieve) ...
- 【NYOJ42】一笔画问题
一笔画问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 Position:http://acm.nyist.net/JudgeOnline/problem.php?pid= ...