English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
下面是一个示例,它使用了结果集章节中描述的几个 getInt 和 getString 方法。此示例与导航结果集部分中解释的前面示例非常相似。
该示例代码是根据前面章节中的环境和数据库设置编写的。
复制并粘贴以下示例到 JDBCExample.java 中,如下编译并运行:
// 步骤 1.导入所需的软件包 import java.sql.*; public class JDBCExample { // JDBC 驱动程序名称和数据库 URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 数据库凭证 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // 步骤 2:注册 JDBC 驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 步骤 3:建立连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 步骤 4:执行查询以创建陈述 // RS 示例的必需参数。 System.out.println("Creating statement..."); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql; sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //تحريك المؤشر إلى السطر الأخير. System.out.println("Moving cursor to the last..."); rs.last(); //الخطوة 5: استخراج البيانات من النتائج System.out.println("Displaying record..."); //البحث بواسطة اسم العمود int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //عرض القيم System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //تحريك المؤشر إلى السطر الأول. System.out.println("Moving cursor to the first row..."); rs.first(); //الخطوة 6: استخراج البيانات من النتائج System.out.println("Displaying record..."); //البحث بواسطة اسم العمود id = rs.getInt("id"); age = rs.getInt("age"); first = rs.getString("first"); last = rs.getString("last"); //عرض القيم System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //تحريك المؤشر إلى السطر الأول. System.out.println("Moving cursor to the next row..."); rs.next(); //الخطوة 7: استخراج البيانات من النتائج System.out.println("Displaying record..."); id = rs.getInt("id"); age = rs.getInt("age"); first = rs.getString("first"); last = rs.getString("last"); //عرض القيم System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); //الخطوة 8: تنظيف البيئة rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //معالجة أخطاء JDBC se.printStackTrace(); } //معالجة خطأ Class.forName e.printStackTrace(); } //المنع النهائي لاستخدام المصادر try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("وداعاً!"); } // تنتهي تعليمات JDBCExample
الآن دعونا نكتب المثال السابق، كما يلي:
C:\>javac JDBCExample.java C:\>
في وقت التشغيلJDBCExample،سيكون هناك نتيجة كما يلي -
C:\>java JDBCExample الاتصال بقاعدة البيانات... إنشاء تعليمات... تحريك المؤشر إلى السطر الأخير... عرض السجل... ID: 103, Age: 30, First: Sumit, Last: Mittal تحريك المؤشر إلى السطر الأول... عرض السجل... ID: 100, Age: 18, First: Zara, Last: Ali تحريك المؤشر إلى السطر التالي... عرض السجل... ID: 101, Age: 25, First: Mahnaz, Last: Fatma وداعاً! C:\>