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()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)