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 ...
随机推荐
- one troubleshooting case about em access issue
Today when trying to open my Oracle EM url, I can not open it. So I thought may be the network is ha ...
- ubuntu How do I configure proxies without GUI?
想法: 我的想法是想是一台国内的 ubuntu 云主机可以通过另外一台在国外(新加坡)的服务器 ,来实现可以访问 google ,哈哈,比较好查资料:) 下面的做法 去修改 /etc/environ ...
- 2 instances of postgresql but I really need one [closed]
I happen to have 2 installed instances of postgresql at my machine: 9.1 and 9.2: sudo service postgr ...
- HTTP状态码图示
这里总结下我们日常开发中常用的HTTP状态码,分享一个老外对HTTP状态码形象化用图片表示的网站:https://http.cat/ 总结如下: 表示服务器已经接收到了请求头,并且客户端应该继续发送请 ...
- Andriod开发技巧——Fragment的懒载入
我们在做应用开发的时候.一个Activity里面可能会以viewpager(或其它容器)与多个Fragment来组合使用.而假设每一个fragment都须要去载入数据.或从本地载入.或从网络载入,那么 ...
- 西门子TCP/UDPport
通过TCP和UDP数据传输的不同服务用到了哪些port? func=ll&objid=21874445&nodeid0=10806074&load=treecontent&am ...
- WCF问题集锦:ReadResponse failed: The server did not return a complete response for this request.
今日.对代码进行单元測试时.发现方法GetAllSupplyTypes报例如以下错误: [Fiddler] ReadResponse() failed: The server did not retu ...
- 将jsp页面的<s:iterator>的数据返回到action
jsp: <form method="post" id="createTable"> <table width="98%" ...
- Java Swing Action 动作
Swing包提供了一种非常实用的机制来封装命令,并将它们连接到多个事件源,这就是Action接口.一个动作是一个封装下列内容的对象: × 命令的说明(一个文本字符串和一个可选图标): × 执行命令所需 ...
- cloudstack ---部署的架构
cloudstack跟KVM一起部署的架构 下图是CloudStack跟kvm一起部署的架构: 在每个kvm的宿主机上都需要部署agent程序. cloudstack跟vsphere一起部署的架构 下 ...