Demo entry 6782290

sd

   

Submitted by anonymous on Jan 12, 2019 at 12:44
Language: Java. Code size: 1.1 kB.

package testSql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//测试
public class testsql01 {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			//1.加载驱动
			Class.forName("oracle.jdbc.driver.OracleDriver");
			
			//2.建立连接
			String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
			String user = "scott";
			String password = "tiger";
		
			conn = DriverManager.getConnection(url, user, password);
				
			//3.查询数据库
			 stmt =  conn.createStatement();
			String sql = "select * from emp";
			rs = stmt.executeQuery(sql);
				
			while(rs.next()) {
					System.out.println(rs.getInt(1));
					System.out.println(rs.getString("ename"));
				
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
			conn.close();
			stmt.close();
			rs.close();
		
	}
	}
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).