MySQL for Linux on POWER 开发应用

http://tech.ddvip.com   2008年01月18日    社区交流

内容摘要:第 2 部分的重点是使用某些主流编程语言为 MySQL 开发应用程序,比如 PHP、Java™、C/C++、Python 和 Perl。作为面向在 POWER 上 Linux 中使用 MySQL 的应用程序开发者的一篇简短指南,本文适合那些熟悉自己的系统环境、网络、媒体设备和磁盘资源的 MySQL 开发者和数据库管理员阅读。

  此示例展示的基本代码将连接到 MySQL 数据库并执行查询。

  下面是本示例的完整的 Java 代码:

  清单 2. Java 代码示例

import java.io.*;
import java.util.*;
import java.sql.*;
public class Java_MySQL
{
  public static void display_rs(ResultSet rs) throws SQLException
  {
   try{
     ResultSetMetaData rsmd = rs.getMetaData();
     for (int i = 1; i <= rsmd.getColumnCount(); ++i)
      System.out.print("      " + (rsmd.getColumnName(i)).toUpperCase() );
      System.out.println();
     while ( rs.next() )
     {
      for (int j = 1; j <= rsmd.getColumnCount(); ++j)
      {
        Object obj = rs.getObject(j);
        System.out.print("      " + obj.toString());
      }
      System.out.println();
     }
   }
   catch (SQLException E) {
      System.out.println("SQLException: " + E.getMessage());
      System.out.println("SQLState:   " + E.getSQLState());
      System.out.println("VendorError: " + E.getErrorCode());
      E.printStackTrace();
      System.exit(1);
   }
  }
  public static void main(String args[])
  {
   Statement statement = null;
   Connection connection = null;
   ResultSet resultset;
   String query, prompt, input;
   int choice = -1;
   try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
   
   catch (Exception E) {
     System.err.println("Unable to load driver.");
     E.printStackTrace();
     System.exit(1);
   }
   try {
     String url="jdbc:mysql://localhost/CONTRACTING";
     String username="username";
     String password="password";
     connection=DriverManager.getConnection(url, username, password);
   }
   catch (SQLException E) {
     System.out.println("SQLException: " + E.getMessage());
     System.out.println("SQLState:   " + E.getSQLState());
     System.out.println("VendorError: " + E.getErrorCode());
     E.printStackTrace();
     connection=null;
     System.exit(1);
   }
   prompt = "
      1. Show contents of the table JOB
" +
        "      2. Exit
";
   while(true)
   {
     System.out.println(prompt);
     try {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      input = in.readLine();
      choice = Integer.parseInt(input);
     }
     catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
     }
     try {
      switch (choice)
      {
        case 1:
         query="SELECT * FROM JOB;";
         statement = connection.createStatement();
         resultset = statement.executeQuery(query);
         display_rs(resultset);
         break;
        case 2:
         System.out.println("Bye!");
         System.exit(0);
       default:
         System.err.println("Invalid value entered!");
      }
     }
     catch (SQLException E) {
      System.out.println("SQLException: " + E.getMessage());
      System.out.println("SQLState:   " + E.getSQLState());
      System.out.println("VendorError: " + E.getErrorCode());
      E.printStackTrace();
      connection=null;
      System.exit(1);
     }
   }
  }
}

来源:ibm    作者:Nikolay V. Yevik    责编:豆豆技术应用

正在加载评论...