如何测试jdbc能否成功连接Oracle数据库
1个回答
展开全部
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("失败");
}
}
}
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("失败");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询