用纯java驱动程序连接oracle数据库class.forname(driver)这里的driver如何写

用纯java驱动程序连接oracle数据库class.forname(driver)这里的driver如何写,第1张

连接Oracle数据库的步骤:

1、加载纯Java驱动程序

Classic.forName(“oracle.jdbc.driver.OralceDriver”)

2、与Oracle数据库建立连接

Connection con=DriverManager.getconnection(“jdbc:oracle:thin:@主机host:端口号”,”用户名”,”密码”)

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()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10080392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存