发布网友 发布时间:2022-04-22 06:23
共2个回答
热心网友 时间:2022-04-07 17:52
先从安装了Oracle的数据库服务器中,找到Oracle安装目录,然后将该目录下的jdbc\lib\classes12.jar文件拷贝到WEB发布服务器的某个目录。假设就直接放在C:\根目录下吧,然后把该路径添加到系统--高级--环境变量中变量名为CLASSPATH?的值中,如:
D:\Program Files\SQLLIB\java\db2java.zip;D:\Program Files\SQLLIB\java\runtime.zip;c:classes12.jar; 也就是让java能够找到这个包。
1.注册加载驱动:
驱动名:DRIVER="oracle.jdbc.driver.OracleDriver";
Class.forName("驱动类名");
2.获得连接:
数据库地址: URL="jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
Connection conn = DriverManager.getConnection(数据库地址,用户名,密码);
System.out.println("连接成功");
3.创建Statement对象:
Statement 类的主要是用于执行静态 SQL 语句并返回它所生成结果的对象。通过Connection 对象的 createStatement()方法可以创建一个Statement对象。例如:Statement statament = connection.createStatement(); 具体示例创建Statement对象
Statement statamentMySQL =connectMySQL.createStatement();
热心网友 时间:2022-04-07 19:10
import java.sql.*;
public class getMetaData {
public static void main(String[] args) {
String driverClassName = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@192.101.101.101:1521:orcl";
String password = "test";
String user= "test";
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException ex) {
System.out.println("加载错误!");
}
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("连接成功");
String sql = "select id_ tid,name_ tname1 ,memo remark from tc";
PreparedStatement ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=ps.executeQuery();
ResultSetMetaData md = ps.getMetaData(); //ps.getMetaData();//
for (int i = 1; i <= md.getColumnCount(); i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i));
}
sql = "select max(id_) tid,name_ tname1 ,memo remark from tc group by name_,memo";
ps=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=ps.executeQuery();
md = rs.getMetaData(); //ps.getMetaData();//
for (int i = 1; i <= md.getColumnCount(); i++) {
System.out.println("name:"+md.getColumnName(i)+" label:"+md.getColumnLabel(i));
}
} catch (SQLException ex1) {
System.out.println(ex1);
System.out.println("失败");
}
}
}