jdbc+oracle测试类。

package org.portal.service;

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试连接oracle数据库
* @author wpc
*
*/

public class Test {
//驱动,URL,用户名,密码
private static final String driver = "oracle.jdbc.driver.OracleDriver";
private static final String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static final String username = "zjuser";
private static final String password = "zjpass";

public static Connection getConnection() {
Connection connection = null;
try {
// 加 载驱动
Class.forName(driver);
// 通过驱动管理类获取数据库连接
connection = DriverManager.getConnection(url, username, password);
// 关闭连接
//connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}

// 测试oracle连接是否成功
public static void main(String[] args) throws SQLException {
Connection connection = Test.getConnection();
if (connection != null) {
System.out.println("连接oracle数据库成功:" + connection);
//这里具体查询数据库中的数据,可以直接取出。
/* String sql = "select * FROM BUSER";
Statement st = connection.createStatement();
st.executeQuery(sql);
ResultSet rs = st.executeQuery(sql);
if(rs.next()){
String name = rs.getString("NAME");
System.out.println("name"+name);
}
connection.close();*/
} else {
System.out.println("连接oracle数据库失败!!!");
}


}
}

//要引入jdbc.jar包

https://files.cnblogs.com/files/cyz110/jdbc6.rar

posted @ 2018-04-02 17:15  zzdxw  阅读(125)  评论(0编辑  收藏  举报