当前位置: 技术问答>java相关
怎样从数据库中读出数据?有详细些的资料吗?
来源: 互联网 发布时间:2015-10-30
本文导语: rt | 下载相应jdbc到本地classpath 编写代码,以oracle举例 import java.io.*; import java.sql.*; import java.util.*; public class JDBCtest{ public static void main(String args[]){ //上面两个括号 Vector drivers =...
rt
|
下载相应jdbc到本地classpath
编写代码,以oracle举例
import java.io.*;
import java.sql.*;
import java.util.*;
public class JDBCtest{
public static void main(String args[]){
//上面两个括号
Vector drivers = new Vector();
Connection con;
String driverClassName = "oracle.jdbc.driver.OracleDriver";
String URL = "jdbc:oracle:thin:@172.16.58.160:1521:orc1";
String user = "test2";
String password = "test2";
try { //注册数据库驱动
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
System.out.println("Registered JDBC driver " + driverClassName);
}
catch (Exception e) {
System.out.println("Can't register JDBC driver: " +
driverClassName + ", Exception: " + e);
}
try { //创建连接
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL,user,password);
}
System.out.println("Created a new connection in pool " + user);
String sql = "select * from sms_mo";
Statement stmt = null;
for(int i=0; i
编写代码,以oracle举例
import java.io.*;
import java.sql.*;
import java.util.*;
public class JDBCtest{
public static void main(String args[]){
//上面两个括号
Vector drivers = new Vector();
Connection con;
String driverClassName = "oracle.jdbc.driver.OracleDriver";
String URL = "jdbc:oracle:thin:@172.16.58.160:1521:orc1";
String user = "test2";
String password = "test2";
try { //注册数据库驱动
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
System.out.println("Registered JDBC driver " + driverClassName);
}
catch (Exception e) {
System.out.println("Can't register JDBC driver: " +
driverClassName + ", Exception: " + e);
}
try { //创建连接
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL,user,password);
}
System.out.println("Created a new connection in pool " + user);
String sql = "select * from sms_mo";
Statement stmt = null;
for(int i=0; i