JDBC三种连接方式
2014-09-19 14:51
387 查看
jdbc连接的三种方式
1.Driver
2.DriverManager
3.Class.forName()
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.junit.Test;
public class TestJdbc {
@Test
public void testgetConnect(){
try {
Driver dirver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "root");
Connection connect = dirver.connect(url, info);
System.out.println(connect);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void testgetConnect2() throws SQLException{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
Connection con =DriverManager.getConnection(url,user,password);
System.out.println(con);
}
@Test
public void testGetConnection3(){
Connection con=null;
Statement stmt = null;
int result = -1;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
con =DriverManager.getConnection(url,user,password);
stmt = con.createStatement();
String sql ="CREATE TABLE `test_address_tmp` (`idtest_address_id` int(11) NOT NULL, `address_name` varchar(45) DEFAULT NULL, PRIMARY KEY (`idtest_address_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
result = stmt.executeUpdate(sql);
System.out.println("Create table result = "+ result);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
1.Driver
2.DriverManager
3.Class.forName()
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.junit.Test;
public class TestJdbc {
@Test
public void testgetConnect(){
try {
Driver dirver = new com.mysql.jdbc.Driver();
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "root");
Connection connect = dirver.connect(url, info);
System.out.println(connect);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void testgetConnect2() throws SQLException{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
Connection con =DriverManager.getConnection(url,user,password);
System.out.println(con);
}
@Test
public void testGetConnection3(){
Connection con=null;
Statement stmt = null;
int result = -1;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
con =DriverManager.getConnection(url,user,password);
stmt = con.createStatement();
String sql ="CREATE TABLE `test_address_tmp` (`idtest_address_id` int(11) NOT NULL, `address_name` varchar(45) DEFAULT NULL, PRIMARY KEY (`idtest_address_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
result = stmt.executeUpdate(sql);
System.out.println("Create table result = "+ result);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
相关文章推荐
- Java JDBC Thin Driver 连接 Oracle有三种方式
- 使用JDBC的三种连接方式查表
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- #.Java使用JDBC连接数据库的三种方式
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- jdbc连接数据库的三种方式
- JDBC创建连接的三种不同方式
- java基础-jdbc——三种方式加载驱动建立连接
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- JDBC获取连接的三种工具类封装方式(v1.0)
- hive 三种启动方式及用途,关注通过jdbc连接的启动
- jdbc连接各种数据库方式列表
- Apache HTTP Server 与 Tomcat 的三种连接方式
- Apache与Tomcat的三种连接方式介绍 [摘]
- 主机与ISCSI设备之间三种连接方式
- Apache与Tomcat的三种连接方式介绍
- 各种数据库的jdbc驱动下载及连接方式
- Apache与Tomcat的三种连接方式介绍
- Apache与Tomcat的三种连接方式介绍
- Apache HTTP Server 与 Tomcat 的三种连接方式介绍息