如何用java语言写一个连接oracle 数据库的程序工具类
1个回答
展开全部
一个最简单连接oracle的例子
package oracletest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
public static void main(String[] args) {
//String serverName = "localhost";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("1");
String url = "jdbc:oracle:thin:@localhost:1521:ORA9I";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
System.out.println("2");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test1");
while (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
package oracletest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleTest {
public static void main(String[] args) {
//String serverName = "localhost";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("1");
String url = "jdbc:oracle:thin:@localhost:1521:ORA9I";
Connection conn = DriverManager.getConnection(url, "scott", "tiger");
System.out.println("2");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test1");
while (rs.next()) {
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询