Пример работы с базой данных в java

code: #java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
 
/**
 * The Class Main.
 *
 * Created on: 20.09.2011
 *
 * @author: M128K145
 */
public class Main {
 
   /**
    * The main method.
    *
    * @param args
    *           the arguments
    */
   public static void main(String[] args) {
 
      try {
         Class.forName("oracle.jdbc.driver.OracleDriver");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
      String url = "jdbc:oracle:thin:@//localhost:1521/M128K145DB";
      Scanner sc = new Scanner(System.in);
      System.out.print("Login: ");
      String login = sc.next();
      System.out.print("Password: ");
      String password = sc.next();
      System.out.println("\nResult:");
      try {
         Connection conn = DriverManager.getConnection(url, login, password);
         Statement st = conn.createStatement();
         ResultSet rs = st.executeQuery("SELECT 'Some value' FROM DUAL");
         while (rs.next()) {
            System.out.println(rs.getString(1));
         }
         st.close();
         conn.close();
      } catch (SQLException e) {
         e.printStackTrace();
      }
 
   }
}

автор: M128K145

Поделиться:

Похожие статьи: