JDBC
一、创建maven工程,导入相应的jar包
因为访问数据库需要用到专门的第三方工具类的jar包,所以我们在使用时需要引入外部jar包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
二、创建mysql.properties并放入resources资源包中
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.118.130:3306/kb10mysql?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=javakb10
三、建一个BaseConfig类,写一个初始化各个配置项的方法,然后用构造器调取该方法作为访问该类的初始化参数
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
class BaseConfig {
private static class Config{
String driver;
String url;
String username;
String password;
public Config(String driver, String url, String username, String password) {
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
}
private Config config;
{
try {
init();
Class.forName(config.driver);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
//初始化各个配置的方法中,要用到Tread.currentTread().getContextClassLoader().gerResource(“file_name”).getPath()方法获取到文件路径,然后使用专门读取配置文件的类Properties(new一个对象)下面的load方法读取这个这个路径下的方法;接下来,使用properties类下的getProperty方法依次读取“url”“driver”“username”“password”;最后把得到的参数传给当前对象。
private void init() throws Exception {
String path = Thread.currentThread().getContextClassLoader().getResource("mysql.properties").getPath();
Properties pro = new Properties();
pro.load(new FileReader(path));
String url = pro.getProperty("url");
if(null==url){
throw new Exception("缺少url配置项异常");
}
String driver = pro.getProperty("driver","com.mysql.jdbc.Driver");
String username = pro.getProperty("username","root");
String password = pro.getProperty("password","root");
this.config = new Config(driver,url,username,password);
}
//创建一个注册驱动的getCon()连接方法,把初始化进来的参数传到DriverManager.getConnection方法里;
protected Connection getCon() throws SQLException {
return DriverManager.getConnection(config.url,config.username,config.password);
}
//创建一个关闭资源释放资源的close()方法
protected void close(AutoCloseable...acs){
for (AutoCloseable ac :acs){
if (null != ac){
try {
ac.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
四、执行类BaseDao
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public final class BaseDao extends BaseConfig{
//预编译的方法
private PreparedStatement getPst(Connection con,String sql, Object...params) throws SQLException {
PreparedStatement pst = con.prepareStatement(sql);
if (params.length>0){
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1,params[i]);
}
}
return pst;
}
//增删改方法
public Result exeNonQuery(String sql,Object...params){
Connection con = null;
PreparedStatement pst = null;
try {
con = getCon();
pst = getPst(con,sql,params);
return Result.succeed(pst.executeUpdate());
} catch (SQLException e) {
return Result.fail();
}finally {
close(pst,con);
}
}
//查询方法
public Result exeQuery(String sql,Object...params){
Connection con =null;
PreparedStatement pst = null;
ResultSet rst = null;
try {
con = getCon();
pst = getPst(con,sql,params);
rst = pst.executeQuery();
List<List<String>> data = new ArrayList<>();
if (null!=rst&&rst.next()){
final int CC = rst.getMetaData().getColumnCount();
do{
List<String> row = new ArrayList<>(CC);
for (int i = 1; i <=CC ; i++) {
row.add(rst.getObject(i).toString());
}
data.add(row);
}while (rst.next());
}
return Result.succeed(data);
}catch (Exception e){
return Result.fail();
}finally {
close(rst,pst,con);
}
}
}
五、结果集类
public final class Result<T> {
private boolean err;
private T data;
public static <T> Result succeed(T data){
return new Result(false,data);
}
public static Result fail(){
return new Result(true,null);
}
private Result(boolean err,T data){
this.err = err;
this.data = data;
}
public boolean isErr() {
return err;
}
public T getData() {
return data;
}
}
六、测试
import java.sql.Types;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
BaseDao dao = new BaseDao();
Result<List<List<String>>> result = dao.exeQuery("select * from student");
if(!result.isErr()){
List<List<String>> data = result.getData();
for (List<String> row : data) {
for (String col : row){
System.out.print(col+"\t");
}
System.out.println();
}
}else {
System.out.println("查询异常");
}
}
}
效果
HDBC
一、poml依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-auth</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
二、创建datasource.properties并放入resources资源包中,再新添加log4j.properties
driver=org.apache.hive.jdbc.HiveDriver
url=jdbc:hive2://192.168.118.130:10000/default
username=root
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/hadoop.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
三、初始化资源类
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BaseConfig {
private class Config{
private String driver;
private String url;
private String username;
private String password;
}
private Config config;
//判断路径是否可用
private boolean valid(String url){
Pattern p = Pattern.compile("jdbc:\\w+://((\\d{1,3}\\.){3}\\d{1,3}|\\w+):\\d{1,5}/\\w+");
Matcher m = p.matcher(url);
return m.matches();
}
//
private void init() throws Exception {
String path = Thread.currentThread().getContextClassLoader()
.getResource("datasource.properties").getPath();
Properties pro = new Properties();
pro.load(new FileReader(path));
String url = pro.getProperty("url");
if(null==url || !valid(url)){
throw new Exception("no or invalid url exception");
}
config = new Config();
config.url = url;
config.driver = pro.getProperty("driver","com.mysql.jdbc.Driver");
config.username = pro.getProperty("username","root");
config.password = pro.getProperty("password","");
}
{
try {
init();
Class.forName(config.driver);
} catch (Exception e) {
e.printStackTrace();
}
}
Connection getCon() throws SQLException {
return DriverManager.getConnection(
config.url,
config.username,
config.password);
}
void close(AutoCloseable...closeables){
for (AutoCloseable closeable : closeables) {
if(null != closeable){
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
四、执行类
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BaseDao extends BaseConfig {
private PreparedStatement getPst(Connection con,String sql, Object...params) throws SQLException {
PreparedStatement pst = con.prepareStatement(sql);
if(params.length>0){
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1,params[i]);
}
}
return pst;
}
public Result exeNonQuery(String sql,Object...params){
Connection con = null;
PreparedStatement pst = null;
try {
con = getCon();
pst = getPst(con,sql,params);
pst.execute();
return Result.succeed();
} catch (SQLException e) {
e.printStackTrace();
return Result.fail();
} finally {
close(pst,con);
}
}
public Result exeQuery(String sql,Object...params){
Connection con = null;
PreparedStatement pst = null;
ResultSet rst = null;
try {
con = getCon();
pst = getPst(con,sql,params);
rst = pst.executeQuery();
List<List<String>> table = null;
if(null != rst && rst.next()){
table = new ArrayList<>();
final int COL = rst.getMetaData().getColumnCount();
do{
List<String> row = new ArrayList<>(COL);
for (int i = 1; i <=COL ; i++) {
row.add(rst.getObject(i).toString());
}
table.add(row);
}while (rst.next());
}
return Result.succeed(table);
} catch (SQLException e) {
e.printStackTrace();
return Result.fail();
}finally {
close(rst,pst,con);
}
}
//用来读取sql语句
public String readSql(String...paths) throws Exception {
String path = paths.length==0 ? "sql/sql.sql" : paths[0];
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
while (null != (line=reader.readLine())){
builder.append(line.trim()+" ");
}
return builder.toString();
}
}
五、结果集类
public abstract class Result<T> {
private boolean err;
private T data;
public static Result fail(){
return new Result(true) {};
}
public static <T> Result succeed(T...t){
return new Result(false,t) {};
}
private Result(boolean err, T...data) {
this.err = err;
this.data = data.length>0 ? data[0] : null;
}
public boolean isErr() {
return err;
}
public T getData() {
return data;
}
}
测试
select * from pv limit 10