- Java连接Oracle实例
package com.rs.cloud.business.cache.test;
import java.sql.*;
public class ControllerOracle {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("加载数据库驱动成功");
String url = "jdbc:oracle:thin:@192.168.168.105:1521:ZLORANEW";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功"+conn);
Inquire inquire=new Inquire();
inquire.gitInquire(conn,"dev_tab_d");
conn.close();
System.out.println("关闭数据库成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
- Java连接Mysql实例
package com.rs.cloud.business.cache.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ControllerMysql {
public static void main(String[] args) {
final String jdbc_driver = "com.mysql.jdbc.Driver";
final String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
final String user = "root";
final String password = "root";
Connection conn = null;
try {
Class.forName(jdbc_driver);
conn = DriverManager.getConnection(db_url, user, password);
Inquire inquire=new Inquire();
inquire.gitInquire(conn,"sys_user");
System.out.println("Mysql连接成功"+conn);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
- 查询接口(Oracle+Mysql)都通用
package com.rs.cloud.business.cache.test;
import java.sql.*;
public class Inquire {
public static String gitInquire(Connection conn, String tabname){
try {
String sql = "select *from " + tabname;
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rset = pstmt.executeQuery();
ResultSetMetaData rsmd = rset.getMetaData();
while (rset.next()) {
for (int i = 1; i < rsmd.getColumnCount(); i++) {
String columnValue = rset.getString(i);
System.out.println("获取指定列的列值:\t" + columnValue);
String colunmName = rsmd.getColumnName(i);
System.out.println("获取指定列的列名:\t" + colunmName);
int columnType = rsmd.getColumnType(i);
System.out.println("获得指定列的数据类型:\t" + columnType);
String columnTypeName = rsmd.getColumnTypeName(i);
System.out.println("获取指定列的数据类型名:\t" + columnTypeName);
String catalogName = rsmd.getColumnClassName(i);
System.out.println("所在的Catalog名字:\t" + catalogName);
String columnClassName = rsmd.getColumnClassName(i);
System.out.println("对应数据类型的类:\t" + columnClassName);
int columnDisplaySize = rsmd.getColumnDisplaySize(i);
System.out.println("在数据库中类型的最大字符个数:\t" + columnDisplaySize);
String columnLabel = rsmd.getColumnLabel(i);
System.out.println("默认的列的标题:\t" + columnLabel);
String schemaName = rsmd.getSchemaName(i);
System.out.println("获得列的模式:\t" + schemaName);
int precision = rsmd.getPrecision(i);
System.out.println("某列类型的精确度(类型的长度):\t" + precision);
int scale = rsmd.getScale(i);
System.out.println("小数点后的位数:\t" + scale);
String tableName = rsmd.getTableName(i);
System.out.println("获取某列对应的表名:\t" + tableName);
boolean isAutoInctement = rsmd.isAutoIncrement(i);
System.out.println("是否自动递增:\t" + isAutoInctement);
boolean isCurrency = rsmd.isCurrency(i);
System.out.println("在数据库中是否为货币型:\t" + isCurrency);
int isNullable = rsmd.isNullable(i);
System.out.println("是否为空:\t" + isNullable);
boolean isReadOnly = rsmd.isReadOnly(i);
System.out.println("是否为只读:\t" + isReadOnly);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return "查询完成";
}
}
- 接口的实现
String sql="待执行的语句"; // 例如 Alter table cxj drop (COM_CODE)
Statement stmt= conn.createStatement(); //创建一个Statement对象
stmt.executeUpdate(sql); //执行sql语句