url路径就是jdbc:oracle:@+主机的ip地址(本机数据库就可以用localhost):加端口号:加数据库名字
package com.jdbc.testimport java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement
public class JDBCTest {
public static void main(String[] args) throws Exception {
//1.加载驱动
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
//Class.forName("com.mysql.jdbc.Driver")
Class.forName("oracle.jdbc.driver.OracleDriver")
//2.创建数据库连接对象
//Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433databaseName=db","sa","sqlpass")
//Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8","root","123456")
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","Oracle123")
//3.创建数据库命令执行对象
Statement stmt = conn.createStatement()
// PreparedStatement ps = conn.prepareStatement("select * from t_user")
//4.执行数据库命令
ResultSet rs = stmt.executeQuery("select * from t_user")
// ResultSet rs = ps.executeQuery()
//5.处理执行结果
while (rs.next()) {
int id = rs.getInt("id")
String username = rs.getString("username")
String password = rs.getString("password")
System.out.println(id + "\t" + username + "\t" + password)
}
//6.释放数据库资源
if (rs != null) {
rs.close()
}
// if (ps != null) {
// ps.close()
// }
if (stmt != null) {
stmt.close()
}
if (conn != null) {
conn.close()
}
}
}
这是以前写的3中数据库的链接已经测试过了。
private static String url="jdbc:oracle:thin:@localhost:1521:xe"声明一个字符串用于存储数据库连接信息,jdbc:oracle:thin:@localhost:1521表示你要连接的是oracle数据库地址是本机
xe为本机数据库库名。
private static String driverName="oracle.jdbc.driver.OracleDriver"
这一条是声明一个字符串存储数据库驱动
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)