JDBC创建连接的三种不同方式
2017-09-19 13:39
281 查看
[align=left] 第一种:
[/align]
//第一步:引包
import java.sql.*;
public class Jdbc {
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false"; //MySQL在高版本需要指明是否进行SSL连接
static final String user = "root";
static final String password = "root";
public static void main(String[] args){
Connection conn = null;
Statement st = null;
try {
//第二步:注册驱动
Class.forName("com.mysql.jdbc.Driver");
//第三步:创建sql语句
String sql = "select id,username,age,sex,phone from sys_user";
//第四步:创建连接
conn = DriverManager.getConnection(DB_URL, user, password); //url,username,password三个参数
//第五步:执行查询
st = conn.createStatement(); //无参
ResultSet rs = st.executeQuery(sql);
//第六步:处理结果集
while (rs.next()) {
//通过列名获取值
int id = rs.getInt("id");
String username = rs.getString("username");
int age = rs.getInt("age");
String phone = rs.getString("phone");
//打印
System.out.print("ID: " + id);
System.out.print(", 年龄: " + age);
System.out.print(", 姓名: " + username);
System.out.println(", 电话: " + phone);
}
rs.close();
}catch (SQLException se){
se.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
//第七步 释放资源
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
第二种:原理同上。
[/align]
//第一步:引包
import java.sql.*;
public class Jdbc {
static final String JDBC_Driver = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/jdbcdemo?useSSL=false"; //MySQL在高版本需要指明是否进行SSL连接
static final String user = "root";
static final String password = "root";
public static void main(String[] args){
Connection conn = null;
Statement st = null;
try {
//第二步:注册驱动
Class.forName("com.mysql.jdbc.Driver");
//第三步:创建sql语句
String sql = "select id,username,age,sex,phone from sys_user";
//第四步:创建连接
conn = DriverManager.getConnection(DB_URL, user, password); //url,username,password三个参数
//第五步:执行查询
st = conn.createStatement(); //无参
ResultSet rs = st.executeQuery(sql);
//第六步:处理结果集
while (rs.next()) {
//通过列名获取值
int id = rs.getInt("id");
String username = rs.getString("username");
int age = rs.getInt("age");
String phone = rs.getString("phone");
//打印
System.out.print("ID: " + id);
System.out.print(", 年龄: " + age);
System.out.print(", 姓名: " + username);
System.out.println(", 电话: " + phone);
}
rs.close();
}catch (SQLException se){
se.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}finally {
//第七步 释放资源
try {
if(st != null){
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
第二种:原理同上。
static final String DB_URL = "jdbc:mysql://localhost:3306/jdbcdemo?user=root&password=root";
conn = DriverManager.getConnection(DB_URL); //url一个参数 第三种:
Properties info = new Properties(); //定义Properties对象 info.setProperty("user","root"); info.setProperty("password","root"); //设置Properties对象属性
conn = DriverManager.getConnection(DB_URL,info); //url,info (info:是一个持久的属性集对象,包括user和password属性)
相关文章推荐
- jdbc mysql数据库链接 及 三种不同的注册驱动的方式
- JDBC三种连接方式
- 在AE中创建关系类(RelatinshipClass)的三种不同方式
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- jdbc连接数据库的三种方式
- JDBC连接数据库(三种不同的数据库)(Oracle、SQLServer、MySQL)
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- iOS开发基础之用三种不同方式创建Table View应用
- SignalR代理对象异常:Uncaught TypeError: Cannot read property 'client' of undefined 推出的结论 SignalR 简单示例 通过三个DEMO学会SignalR的三种实现方式 SignalR推送框架两个项目永久连接通讯使用 SignalR 集线器简单实例2 用SignalR创建实时永久长连接异步网络应用程序
- 关于数据库连接池创建(楼主采用JDBC+ODBC桥接方式连接数据库)
- 加载jdbc驱动程序的三种不同方式
- 不同数据库jdbc连接方式
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- RMAN的两种连接方式(每种连接方式的三种不同连接方法)
- PHP连接数据库、创建数据库、创建表的三种方式
- JDBC不同数据库的连接方式
- Intelli JDEA 创建Scala项目的三种不同方式
- iOS开发基础之用三种不同方式创建Table View应用
- 【Java EE 学习 52】【Spring学习第四天】【Spring与JDBC】【JdbcTemplate创建的三种方式】【Spring事务管理】【事务中使用dbutils则回滚失败!!!??】
- #.Java使用JDBC连接数据库的三种方式