当前位置:首页 » 数据仓库 » java连接oracle数据库测试
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

java连接oracle数据库测试

发布时间: 2023-06-04 08:46:58

1. java 连接oracle

给你一个能看懂的。
记得给兄我多加点分。谢谢了啊。
# 1、Oracle8/8i/9i数据库(thin模式)
# Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
# String url="jdbc:oracle:thin:@localhost:1521:orcl";
# //orcl为数据库的SID
# String user="test";
# String password="test";
# Connection conn= DriverManager.getConnection(url,user,password);
#
# 2、DB2数据库
# Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
# String url="jdbc:db2://localhost:5000/sample";
# //sample为你的数据库名
# String user="admin";
# String password="";
# Connection conn= DriverManager.getConnection(url,user,password);
#
# 3、sql Server7.0/2000数据库
# Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
# String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
# //mydb为数据库
# String user="sa";
# String password="";
# Connection conn= DriverManager.getConnection(url,user,password);
#
# 4、Sybase数据库
# Class.forName("com.sybase.jdbc.SybDriver").newInstance();
# String url =" jdbc:sybase:Tds:localhost:5007/myDB";
# //myDB为你的数据库名
# Properties sysProps = System.getProperties();
# SysProps.put("user","运派userid");
# SysProps.put("password","user_password");
# Connection conn= DriverManager.getConnection(url, SysProps);
#
# 5、Informix数据库
# Class.forName("com.informix.jdbc.IfxDriver"孝大).newInstance(); String url =
# "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
# user=testuser;password=testpassword";
# //myDB为数据库名
# Connection conn= DriverManager.getConnection(url);
#
# 6、MySQL数据旁慎贺库
# Class.forName("org.gjt.mm.mysql.Driver").newInstance();
# String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
# //myDB为数据库名
# Connection conn= DriverManager.getConnection(url);
#
# 7、PostgreSQL数据库
# Class.forName("org.postgresql.Driver").newInstance();
# String url ="jdbc:postgresql://localhost/myDB"
# //myDB为数据库名
# String user="myuser";
# String password="mypassword";
# Connection conn= DriverManager.getConnection(url,user,password);

2. 如何用Java实现连接Oracle

1、工作环境:myeclipse中->新建java_oracle工程->新建包com.zp->新建java_oracle_jdbc.java类
2、需要配置Oracle数据库驱动
java工程(java_oracle)—>右键属性(Properties)->Java Build Path->Libraries->Add External JARs->添加oracle安装目录E:\oracle\proct\10.1.0\Db_1\jdbc\lib下的“classes12.jar”文件;
每次新建一个不同工程都需要配置一次
3、代码如下:
package com.zp;
import java.sql.*;
public class java_oracle_jdbc {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到连接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:simlink", "scott", "tiger");

Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select * from emp");
while(rs.next())
{
System.out.println("用户名:"+rs.getString(2));
}
rs.close();
sm.close();
ct.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

3. 怎么实现用java访问oracle数据库

实现用java访问oracle数据库:

准备工作:

1.下载驱动,打开浏览器,搜索oracle数据库驱动,进入下载页面下载即可;

2.将驱动导入到项目中。

用java访问oracle数据库代码实现:



try{
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
//与数据库建立连接
try{
Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";
Stringuser="scott";//用户名
Stringpassword="tiger";//密码
conn=DriverManager.getConnection(url,username,password);
}catch(SQLExceptione){
e.printStackTrace();
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}

4. 如何用java连接oracle数据库

public class OracleJdbcTest
{
String driverClass = "oracle.jdbc.driver.OracleDriver";

Connection con;

public void init(FileInputStream fs) throws Exception
{
Properties props = new Properties();
props.load(fs);
String url = props.getProperty("db.url");
String userName = props.getProperty("db.user");
String password = props.getProperty("db.password");
Class.forName(driverClass);

con=DriverManager.getConnection(url, userName, password);
}

public void fetch() throws SQLException, IOException
{
PreparedStatement ps = con.prepareStatement("select SYSDATE from al");
ResultSet rs = ps.executeQuery();

while (rs.next())
{
// do the thing you do
}
rs.close();
ps.close();
}

public static void main(String[] args)
{
OracleJdbcTest test = new OracleJdbcTest();
test.init();
test.fetch();
}
}

[代码] db.properties
db.url = jdbc:oracle:thin:@::
db.user = sa
db.password =

5. 求一个连接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("失败");
}
}
}

6. JAVA JDBC连接Oracle数据库 进行压力测试

你好,你的UserDao第46行已经报空指针了,还有Connection reset是估计你的连接池资源不够达到上限了!

7. java与oracle数据库的连接

程序运行时是不需要myeclipse与数据库作连接斗行仿得,程序连接orcle的代带晌码最下楼得哥们都贴出来了,借来用用.

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();

Stringurl="jdbc:oracle:thin:@localhost:1521:orcl";

//orcl为数据库的SID

Stringuser="test";

Stringpassword="test";

Connectionconn=DriverManager.getConnection(url,user,password);

myeclipse得数据库连接相当于数据库得客户端,用来直接操作或查看数据库使用得,当然,也可以产生一些orm映射代码,非常方便.

首先选择myeclipsedatabaseexplorer打开数据库窗口,参看图片一

然空纤后配置到oracle得连接,我晕,只能上传一个图片.

哥们,邮件给我,我发给你[email protected]

8. 怎么用java连接oracle数据库,需要详细解释

1)首先你要有java连接oracle数据库的驱动类,下载好后导入到工程里

2)代码:
import java.sql.*;

public class JDBCTest {

/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args)
throws ClassNotFoundException, SQLException{
//1.准备参数
String ip = "192.168.0.26";
String sid = "tarena";
String port = "1521";
String dbUser = "openlab";
String dbPassword = "open123";
String driver
= "oracle.jdbc.driver.OracleDriver";

//2.构造驱动实例
Class.forName(driver);

//3.创建连接
//连接字符串是固定的形式,oracle的形式:
String url
= "jdbc:oracle:thin:@"
+ ip + ":" + port + ":" + sid;
Connection conn
= DriverManager.getConnection
(url, dbUser, dbPassword);
//4.执行SQL语句
String sql = "select id, password, name from user_sd1104 " +
"where id = 1001 and password = '1234'";
Statement stmt = conn.createStatement();
ResultSet rs
= stmt.executeQuery(sql);//执行sql语句
while(rs.next()){
int id = rs.getInt(1);
String pwd = rs.getString(2);
String name = rs.getString(3);
System.out.println("welcome," + name);
}
rs.close();
stmt.close();
conn.close();
}

}