java连接jdbc读取oracle数据库的内容
2012-06-05 14:09:23 来源:WEB开发网核心提示:7.下面代码可以查询数据库种的内容:package oracle.ResultSetDemo01.org;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statemen
7.下面代码可以查询数据库种的内容: package oracle.ResultSetDemo01.org; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ConnectJdbc { //驱动程序就是之前在classpath中配置的jdbc的驱动程序jar中 public static final String drive = "oracle.jdbc.driver.OracleDriver"; /** * 连接地址,各个厂商提供单独记住 * jdbc:oracle:thin:@localhost:1521:ORCL localhost 是ip地址。 */ public static final String url = "jdbc:oracle:thin:@192.168.1.15:1521:ORCL"; /** * 用户 密码 */ public static final String DBUSER="gyly"; public static final String password="gyly"; public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Connection conn = null;//表示数据库连接 Statement stmt= null;//表示数据库的更新 ResultSet result = null;//查询数据库 Class.forName(drive);//使用class类来加载程序 conn =DriverManager.getConnection(url,DBUSER,password); //连接数据库 //Statement接口要通过connection接口来进行实例化操作 stmt = conn.createStatement(); //执行SQL语句来查询数据库 result =stmt.executeQuery("SELECT DEPTID,DEPTNAME,ADDR,TEL,FAX,LXR,PX,UNITID FROM bs_dept"); while(result.next()){//判断有没有下一行 int DEPTID =result.getInt(1); String DEPTNAME = result.getString(2); String ADDR = result.getString(3); long TEL = result.getLong(4); long FAX = result.getLong(5); String LXR =result.getString(6); int PX =result.getInt(7); int UNITID = result.getInt(8); System.out.print("DEPTID="+DEPTID+";"); System.out.print("DEPTNAME="+DEPTNAME+";"); System.out.print("ADDR="+ADDR+";"); System.out.print("TEL="+TEL+";"); System.out.print("FAX="+FAX+";"); System.out.print("LXR="+LXR+";"); System.out.print("PX="+PX+";"); System.out.print("UNITID="+UNITID+";"); System.out.println(); } //System.out.println(conn); result.close();//数据库先开后关 stmt.close(); conn.close();//关闭数据库 } }
7.下面代码可以查询数据库种的内容: package oracle.ResultSetDemo01.org; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class ConnectJdbc { //驱动程序就是之前在classpath中配置的jdbc的驱动程序jar中 public static final String drive = "oracle.jdbc.driver.OracleDriver"; /** * 连接地址,各个厂商提供单独记住 * jdbc:oracle:thin:@localhost:1521:ORCL localhost 是ip地址。 */ public static final String url = "jdbc:oracle:thin:@192.168.1.15:1521:ORCL"; /** * 用户 密码 */ public static final String DBUSER="gyly"; public static final String password="gyly"; public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Connection conn = null;//表示数据库连接 Statement stmt= null;//表示数据库的更新 ResultSet result = null;//查询数据库 Class.forName(drive);//使用class类来加载程序 conn =DriverManager.getConnection(url,DBUSER,password); //连接数据库 //Statement接口要通过connection接口来进行实例化操作 stmt = conn.createStatement(); //执行SQL语句来查询数据库 result =stmt.executeQuery("SELECT DEPTID,DEPTNAME,ADDR,TEL,FAX,LXR,PX,UNITID FROM bs_dept"); while(result.next()){//判断有没有下一行 int DEPTID =result.getInt(1); String DEPTNAME = result.getString(2); String ADDR = result.getString(3); long TEL = result.getLong(4); long FAX = result.getLong(5); String LXR =result.getString(6); int PX =result.getInt(7); int UNITID = result.getInt(8); System.out.print("DEPTID="+DEPTID+";"); System.out.print("DEPTNAME="+DEPTNAME+";"); System.out.print("ADDR="+ADDR+";"); System.out.print("TEL="+TEL+";"); System.out.print("FAX="+FAX+";"); System.out.print("LXR="+LXR+";"); System.out.print("PX="+PX+";"); System.out.print("UNITID="+UNITID+";"); System.out.println(); } //System.out.println(conn); result.close();//数据库先开后关 stmt.close(); conn.close();//关闭数据库 } }
更多精彩
赞助商链接