Java访问oracle/vertica数据库示例(连接调试用)
Java对于jdbc对数据库的封装,封装方法参考:
1、Java(32):Java对于jdbc对数据库的封装(Bean)https://blog.csdn.net/fen_fen/article/details/120518266
2、Java(31):Java对于jdbc对数据库的封装[2]https://blog.csdn.net/fen_fen/article/details/120469074
3、Java(23):Java对于jdbc对数据库的封装[1]
https://blog.csdn.net/fen_fen/article/details/120379997
一、Java访问oracle数据库示例
1、Maven引用(或直接导入jar包)
<!-- oracle依赖-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
2、连接oracle数据库配置
static final String JDBC_DRIVER="oracle.jdbc.OracleDriver";
static final String DB_URL="jdbc:oracle:thin:@10.1.1.232:1521/ORCL";
static final String USER = "HMF";
static final String PASSWORD = "lianshi";
3、连接oracle数据库示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class oracleSqlTest {
static final String JDBC_DRIVER="oracle.jdbc.OracleDriver";
static final String DB_URL="jdbc:oracle:thin:@10.1.1.232:1521/ORCL";
static final String USER = "HMF";
static final String PASSWORD = "lianshi";
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try{
//注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
//打开链接
System.out.println("Connect DB...");
conn= DriverManager.getConnection(DB_URL,USER,PASSWORD);
//执行查询
System.out.println("实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM aoedata";
ResultSet rs = stmt.executeQuery(sql);
//展开结果集数据库
System.out.println("aoeId"+"\t"+"aoeAes"+"\t"+"aoeSm4");
while (rs.next()){
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getString(3) + "\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
4、执行结果
二、Java访问vertica数据库示例
1、Maven引用(或直接导入jar包)
<dependency>
<groupId>com.vertica.jdbc</groupId>
<artifactId>vertica-jdbc</artifactId>
<version>10.0.1-0</version>
</dependency>
2、连接vertica数据库配置
static final String JDBC_DRIVER="com.vertica.jdbc.Driver";
static final String DB_URL="jdbc:vertica://10.1.1.215:5433/verticadb?searchPath=cg";
static final String USER = "dbadmin";
static final String PASSWORD = "lianShi2021";
3、连接vertica数据库示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class verticaSqlTest {
static final String JDBC_DRIVER="com.vertica.jdbc.Driver";
static final String DB_URL="jdbc:vertica://10.1.1.215:5433/verticadb?searchPath=cg";
static final String USER = "dbadmin";
static final String PASSWORD = "lianShi2021";
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try{
//注册 JDBC 驱动
Class.forName(JDBC_DRIVER);
//打开链接
System.out.println("Connect DB...");
conn= DriverManager.getConnection(DB_URL,USER,PASSWORD);
//执行查询
System.out.println("实例化Statement对象...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM aoedata";
ResultSet rs = stmt.executeQuery(sql);
//展开结果集数据库
System.out.println("aoeId"+"\t"+"aoeAes"+"\t"+"aoeSm4");
while (rs.next()){
System.out.print(rs.getInt(1) + "\t");
System.out.print(rs.getString(2) + "\t");
System.out.print(rs.getString(3) + "\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}