熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java操作mssql2000的小例子

2013-11-23 19:10:32  來源: Java核心技術 

    主要由三個文件組成一個是主執行文件一個是用戶界面文件一個是數據模塊文件
用戶界面這一塊是用的eclipse的VE插件做的只是還不知道如何發布這樣的應用程序.唉
 
 
//主程序
package comfcgl;
public class FC {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Autogenerated method stub
       Loginmain(null);
      
     
 }
}

//用戶界面
package comfcgl;
import javasqlResultSet;
import javasqlResultSetMetaData;
import orgeclipseswtSWT;
import orgeclipseswtgraphicsPoint;
import orgeclipseswtgraphicsRectangle;
import orgeclipseswtlayoutFillLayout;
import orgeclipseswtwidgetsButton;
import orgeclipseswtwidgetsComposite;
import orgeclipseswtwidgetsDisplay;
import orgeclipseswtwidgetsLabel;
import orgeclipseswtwidgetsList;
import orgeclipseswtwidgetsShell;
import orgeclipseswtwidgetsText;
public class Login {
 private Shell sShell = null;  //  @jve:declindex=:visualconstraint=
 private Composite composite = null;
 private Button button = null;
 private Dm dm;
 private Label label = null;
 private Text text = null;
 private Text text = null;
 private Text text = null;
 private Text textArea = null;
 private List list = null;
 private Button button = null;
 /**
  * This method initializes composite 
  *
  */
 private void createComposite() {
  composite = new Composite(sShell SWTNONE);
  compositesetLayout(null);
  button = new Button(composite SWTNONE);
  buttonsetText(連接);
  buttonsetSize(new Point( ));
  buttonsetLocation(new Point( ));
  buttonaddSelectionListener(new orgeclipseswteventsSelectionAdapter()
{
   public void widgetSelected(orgeclipseswteventsSelectionEvent e)
{
    dm = new Dm();
    boolean c = dmDconnectionOpen(textgetText
()textgetText()textgetText());
    if (buttongetText() == 連接) {
     if (c == false) {
      labelsetText(連接失敗);
     } else {
      labelsetText(連接成功);
      buttonsetText(斷開);
      ResultSet rs=dmExeSQL(textAreagetText
());
      try{
       listremoveAll();
      while(rsnext()){
       for(int i=;i<=;i++){
             listadd(rsgetString(i));
       }
         }
      }
      catch(Exception ee){
         eeprintStackTrace();
      }
      
     }
    } else {
     boolean d =dmDconnectionClose();
     if (d==false){
      labelsetText(斷開失敗);
     }
     else{
      labelsetText(斷開成功);
      buttonsetText(連接);
      }
     }
     
    
   }
  });
  label = new Label(composite SWTNONE);
  labelsetBounds(new Rectangle( ));
  labelsetText();
  text = new Text(composite SWTBORDER);
  textsetBounds(new Rectangle( ));
  text = new Text(composite SWTBORDER);
  textsetBounds(new Rectangle( ));
  text = new Text(composite SWTBORDER);
  textsetBounds(new Rectangle( ));
  textArea = new Text(composite SWTMULTI | SWTWRAP | SWTV_SCROLL |
SWTBORDER);
  textAreasetBounds(new Rectangle( ));
  list = new List(composite SWTBORDER);
  listsetBounds(new Rectangle( ));
  button = new Button(composite SWTNONE);
  buttonsetBounds(new Rectangle( ));
  buttonsetText(字段);
  buttonaddSelectionListener(new orgeclipseswteventsSelectionAdapter()
{
   public void widgetSelected(orgeclipseswteventsSelectionEvent e)
{
    listremoveAll();
    ResultSetMetaData sm=dmGetField();
    try{
    for (int i=;i<=smgetColumnCount();i++){
     listadd(smgetColumnName(i));
    }
    }
    catch(Exception ee){
     eeprintStackTrace();
    }
   }
   });
   
 
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Autogenerated method stub
  /* Before this is run be sure to set up the launch configuration
(Arguments>VM Arguments)
   * for the correct SWT library path in order to run with the SWT dlls
   * The dlls are located in the SWT plugin jar 
   * For example on Windows the Eclipse SWT plugin jar is:
   *       installation_directory\plugins\orgeclipseswtwin_jar
   */
 
  Display display = DisplaygetDefault();
  Login thisClass = new Login();
  thisClasscreateSShell();
  thisClasssShellopen();
  while (!thisClasssShellisDisposed()) {
   if (!displayreadAndDispatch())
    displaysleep();
  }
  displaydispose();
 }
 /**
  * This method initializes sShell
  */
 private void createSShell() {
  sShell = new Shell();
  
  sShellsetText(數據庫連接);
  sShellsetSize(new Point( ));
  sShellsetLayout(new FillLayout());
  /*Button btn =new Button(compositeSWTNone);
  btnsetBounds( );
  btnsetText(連接);*/
  createComposite();
  
  
 }
}
 
//數據模塊
package comfcgl;
import javasqlConnection;
import javasqlDriverManager;
import javasqlResultSet;
import javasqlResultSetMetaData;
import javasqlStatement;

public class Dm {
 Connection dcon;
 Statement stmt;
    ResultSet rs = null;
    ResultSetMetaData rmd;
 public boolean DconnectionOpen(String iString uString p){
  final String jdbc_driver = commicrosoftjdbcsqlserverSQLServerDriver;
  final String database_url =
  jdbc:microsoft:sqlserver://+i+:;DatabaseName=vlliatemp;
  final String user = u;
  final String password = p;
 try{
  ClassforName(jdbc_driver);
   dcon = DriverManagergetConnection(database_url user
                password);
   stmt = dconcreateStatement(ResultSetTYPE_SCROLL_SENSITIVE
              ResultSetCONCUR_UPDATABLE);
  
 } 
 catch (Exception e) {
  eprintStackTrace();
  return false;
  
 }
  return true;
 }
 
 public boolean DconnectionClose(){
  try {
   
   stmtclose();
   dconclose(); 
  } catch (Exception e) {
          eprintStackTrace();
          return false;
  }
  return true;
 }
 
 public ResultSet ExeSQL(String s){
  try{
  rs=stmtexecuteQuery(s);
  }
  catch(Exception e){
   eprintStackTrace();
  }
  return rs;
  
 }
 public ResultSetMetaData GetField(){
  
  try{
   rmd =rsgetMetaData();
    
     }
  catch (Exception e){
   eprintStackTrace();
  
  }
  return rmd;
  
 }
 
}

From:http://tw.wingwit.com/Article/program/Java/hx/201311/26398.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.