求一个连接oracle数据库的java类和一个测试是否连接成功的JAVA类
展开全部
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("失败");
}
}
}
展开全部
public class Test {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("你的数据库连接url",
"你的用户名", "你的密码");
System.out.println("连接成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("没有找到类...");
} catch (SQLException e) {
System.out.println("sql语法错误...");
e.printStackTrace();
}
}
}
最后,别忘了把连接oracle的jdbc jar包加进去哈
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("你的数据库连接url",
"你的用户名", "你的密码");
System.out.println("连接成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("没有找到类...");
} catch (SQLException e) {
System.out.println("sql语法错误...");
e.printStackTrace();
}
}
}
最后,别忘了把连接oracle的jdbc jar包加进去哈
追问
谢谢你
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private static Connection conn;
static {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@ip:1521:orcl" , "username" , "password" );
System.out.println("连接成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@ip:1521:orcl" , "username" , "password" );
System.out.println("连接成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询