标签:移动 style use ddl parameter eve 数据库驱动 指定 ora
//加载驱动
Class.forName(driverClass);
//加载MySQL的驱动
Class.forName("com.mysql.jdbc.Driver");
- //加载Oracle的驱动
- Class.forName("oracle.jdbc.driver.OracleDriver");
//获取数据库连接
DriverManager.getConnection(String url, String user, String password);
//MySQL数据库URL的写法
jdbc:mysql://hostname:port/databasename
//Oracle数据库URL的写法
jdbc:oracle:thin:@hostname:port:databasename
package com.sqq.mystudy.JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
try {
//1、加载MySQL数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、使用DriverManager获取数据库连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mytest",
"root","pwd"
);
//3、使用Connection创建一个Statement对象
Statement stmt = conn.createStatement();
//4、执行SQL语句
ResultSet rs = stmt.executeQuery("SELECT * FROM mtest");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t"
+ rs.getString(2) + "\t\t"
+ rs.getString(3));
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
SET AUTOCOMMIT = {0 | 1} #0为关闭自动提交,即开启事务
//关闭自动提交,开启事务
conn.setAutoCommit(false);
//提交事务
conn.commit();
//回滚事务
conn.rollback();
public static void commitTransaction(String[] sqls) {
//加载驱动
try {
Class.forName(dbPar.getDriver());
Connection conn = null;
try {
conn = DriverManager.getConnection(dbPar.getUrl(),
dbPar.getUser(), dbPar.getPassword());
//关闭自动提交事务
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
for (String sql : sqls) {
System.out.println(sql);
}
//提交事务
conn.commit();
//关闭连接
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
标签:移动 style use ddl parameter eve 数据库驱动 指定 ora
原文地址:http://www.cnblogs.com/sunqiangqiang/p/6914645.html