| 導購 | 订阅 | 在线投稿
分享
 
 
 

java操作mssql2000的小例子

來源:互聯網  2008-05-19 06:34:25  評論

主要由三個文件組成.一個是主執行文件,一個是用戶界面文件,一個是數據模塊文件.

用戶界面這一塊是用的eclipse的VE插件做的,只是還不知道如何發布這樣的應用程序.唉...

//主程序

package com.fcgl;

public class FC {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Login.main(null);

}

}

//用戶界面

package com.fcgl;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import org.eclipse.swt.SWT;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Composite;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Label;

import org.eclipse.swt.widgets.List;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Text;

public class Login {

private Shell sShell = null; // @jve:decl-index=0:visual-constraint="74,10"

private Composite composite1 = null;

private Button button1 = null;

private Dm dm1;

private Label label1 = null;

private Text text1 = null;

private Text text2 = null;

private Text text3 = null;

private Text textArea1 = null;

private List list1 = null;

private Button button2 = null;

/**

* This method initializes composite1

*

*/

private void createComposite1() {

composite1 = new Composite(sShell, SWT.NONE);

composite1.setLayout(null);

button1 = new Button(composite1, SWT.NONE);

button1.setText("連接");

button1.setSize(new Point(48, 22));

button1.setLocation(new Point(176, 159));

button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()

{

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)

{

dm1 = new Dm();

boolean c = dm1.DconnectionOpen(text1.getText

(),text2.getText(),text3.getText());

if (button1.getText() == "連接") {

if (c == false) {

label1.setText("連接失敗");

} else {

label1.setText("連接成功");

button1.setText("斷開");

ResultSet rs=dm1.ExeSQL(textArea1.getText

());

try{

list1.removeAll();

while(rs.next()){

for(int i=1;i<=3;i++){

list1.add(rs.getString(i));

}

}

}

catch(Exception ee){

ee.printStackTrace();

}

}

} else {

boolean d =dm1.DconnectionClose();

if (d==false){

label1.setText("斷開失敗");

}

else{

label1.setText("斷開成功");

button1.setText("連接");

}

}

}

});

label1 = new Label(composite1, SWT.NONE);

label1.setBounds(new Rectangle(297, 163, 83, 12));

label1.setText("");

text1 = new Text(composite1, SWT.BORDER);

text1.setBounds(new Rectangle(176, 16, 90, 17));

text2 = new Text(composite1, SWT.BORDER);

text2.setBounds(new Rectangle(176, 42, 90, 17));

text3 = new Text(composite1, SWT.BORDER);

text3.setBounds(new Rectangle(176, 69, 90, 17));

textArea1 = new Text(composite1, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL |

SWT.BORDER);

textArea1.setBounds(new Rectangle(176, 91, 163, 59));

list1 = new List(composite1, SWT.BORDER);

list1.setBounds(new Rectangle(10, 11, 145, 169));

button2 = new Button(composite1, SWT.NONE);

button2.setBounds(new Rectangle(242, 160, 48, 22));

button2.setText("字段");

button2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()

{

public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)

{

list1.removeAll();

ResultSetMetaData sm=dm1.GetField();

try{

for (int i=1;i<=sm.getColumnCount();i++){

list1.add(sm.getColumnName(i));

}

}

catch(Exception ee){

ee.printStackTrace();

}

}

});

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated 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 3.1 plugin jar is:

* installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar

*/

Display display = Display.getDefault();

Login thisClass = new Login();

thisClass.createSShell();

thisClass.sShell.open();

while (!thisClass.sShell.isDisposed()) {

if (!display.readAndDispatch())

display.sleep();

}

display.dispose();

}

/**

* This method initializes sShell

*/

private void createSShell() {

sShell = new Shell();

sShell.setText("數據庫連接");

sShell.setSize(new Point(391, 221));

sShell.setLayout(new FillLayout());

/*Button btn1 =new Button(composite1,SWT.None);

btn1.setBounds(12, 12, 22, 22);

btn1.setText("連接");*/

createComposite1();

}

}

//數據模塊

package com.fcgl;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.Statement;

public class Dm {

Connection dcon;

Statement stmt;

ResultSet rs = null;

ResultSetMetaData rmd;

public boolean DconnectionOpen(String i,String u,String p){

final String jdbc_driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

final String database_url =

"jdbc:microsoft:sqlserver://"+i+":1433;DatabaseName=vllia-temp";

final String user = u;

final String password = p;

try{

Class.forName(jdbc_driver);

dcon = DriverManager.getConnection(database_url, user,

password);

stmt = dcon.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

}

catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

public boolean DconnectionClose(){

try {

stmt.close();

dcon.close();

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

public ResultSet ExeSQL(String s){

try{

rs=stmt.executeQuery(s);

}

catch(Exception e){

e.printStackTrace();

}

return rs;

}

public ResultSetMetaData GetField(){

try{

rmd =rs.getMetaData();

}

catch (Exception e){

e.printStackTrace();

}

return rmd;

}

}

   主要由三個文件組成.一個是主執行文件,一個是用戶界面文件,一個是數據模塊文件.   用戶界面這一塊是用的eclipse的VE插件做的,只是還不知道如何發布這樣的應用程序.唉...         //主程序   package com.fcgl;   public class FC {   /**    * @param args    */   public static void main(String[] args) {   // TODO Auto-generated method stub    Login.main(null);   }   }      //用戶界面   package com.fcgl;   import java.sql.ResultSet;   import java.sql.ResultSetMetaData;   import org.eclipse.swt.SWT;   import org.eclipse.swt.graphics.Point;   import org.eclipse.swt.graphics.Rectangle;   import org.eclipse.swt.layout.FillLayout;   import org.eclipse.swt.widgets.Button;   import org.eclipse.swt.widgets.Composite;   import org.eclipse.swt.widgets.Display;   import org.eclipse.swt.widgets.Label;   import org.eclipse.swt.widgets.List;   import org.eclipse.swt.widgets.Shell;   import org.eclipse.swt.widgets.Text;   public class Login {   private Shell sShell = null; // @jve:decl-index=0:visual-constraint="74,10"   private Composite composite1 = null;   private Button button1 = null;   private Dm dm1;   private Label label1 = null;   private Text text1 = null;   private Text text2 = null;   private Text text3 = null;   private Text textArea1 = null;   private List list1 = null;   private Button button2 = null;   /**    * This method initializes composite1    *    */   private void createComposite1() {   composite1 = new Composite(sShell, SWT.NONE);   composite1.setLayout(null);   button1 = new Button(composite1, SWT.NONE);   button1.setText("連接");   button1.setSize(new Point(48, 22));   button1.setLocation(new Point(176, 159));   button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()   {   public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)   {   dm1 = new Dm();   boolean c = dm1.DconnectionOpen(text1.getText   (),text2.getText(),text3.getText());   if (button1.getText() == "連接") {   if (c == false) {   label1.setText("連接失敗");   } else {   label1.setText("連接成功");   button1.setText("斷開");   ResultSet rs=dm1.ExeSQL(textArea1.getText   ());    try{    list1.removeAll();   while(rs.next()){   for(int i=1;i<=3;i++){    list1.add(rs.getString(i));   }    }    }    catch(Exception ee){    ee.printStackTrace();    }   }   } else {   boolean d =dm1.DconnectionClose();   if (d==false){   label1.setText("斷開失敗");   }   else{   label1.setText("斷開成功");   button1.setText("連接");   }   }   }   });   label1 = new Label(composite1, SWT.NONE);   label1.setBounds(new Rectangle(297, 163, 83, 12));   label1.setText("");   text1 = new Text(composite1, SWT.BORDER);   text1.setBounds(new Rectangle(176, 16, 90, 17));   text2 = new Text(composite1, SWT.BORDER);   text2.setBounds(new Rectangle(176, 42, 90, 17));   text3 = new Text(composite1, SWT.BORDER);   text3.setBounds(new Rectangle(176, 69, 90, 17));   textArea1 = new Text(composite1, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL |   SWT.BORDER);   textArea1.setBounds(new Rectangle(176, 91, 163, 59));   list1 = new List(composite1, SWT.BORDER);   list1.setBounds(new Rectangle(10, 11, 145, 169));   button2 = new Button(composite1, SWT.NONE);   button2.setBounds(new Rectangle(242, 160, 48, 22));   button2.setText("字段");   button2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()   {   public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)   {   list1.removeAll();   ResultSetMetaData sm=dm1.GetField();   try{   for (int i=1;i<=sm.getColumnCount();i++){   list1.add(sm.getColumnName(i));   }   }   catch(Exception ee){   ee.printStackTrace();   }   }   });   }   /**    * @param args    */   public static void main(String[] args) {   // TODO Auto-generated 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 3.1 plugin jar is:    * installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar    */   Display display = Display.getDefault();   Login thisClass = new Login();   thisClass.createSShell();   thisClass.sShell.open();   while (!thisClass.sShell.isDisposed()) {   if (!display.readAndDispatch())   display.sleep();   }   display.dispose();   }   /**    * This method initializes sShell    */   private void createSShell() {   sShell = new Shell();   sShell.setText("數據庫連接");   sShell.setSize(new Point(391, 221));   sShell.setLayout(new FillLayout());   /*Button btn1 =new Button(composite1,SWT.None);   btn1.setBounds(12, 12, 22, 22);   btn1.setText("連接");*/   createComposite1();   }   }      //數據模塊   package com.fcgl;   import java.sql.Connection;   import java.sql.DriverManager;   import java.sql.ResultSet;   import java.sql.ResultSetMetaData;   import java.sql.Statement;      public class Dm {   Connection dcon;   Statement stmt;    ResultSet rs = null;    ResultSetMetaData rmd;   public boolean DconnectionOpen(String i,String u,String p){   final String jdbc_driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";   final String database_url =   "jdbc:microsoft:sqlserver://"+i+":1433;DatabaseName=vllia-temp";   final String user = u;   final String password = p;   try{   Class.forName(jdbc_driver);    dcon = DriverManager.getConnection(database_url, user,    password);    stmt = dcon.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,    ResultSet.CONCUR_UPDATABLE);   }   catch (Exception e) {   e.printStackTrace();   return false;   }   return true;   }   public boolean DconnectionClose(){   try {   stmt.close();   dcon.close();   } catch (Exception e) {    e.printStackTrace();    return false;   }   return true;   }   public ResultSet ExeSQL(String s){   try{   rs=stmt.executeQuery(s);   }   catch(Exception e){   e.printStackTrace();   }   return rs;   }   public ResultSetMetaData GetField(){   try{   rmd =rs.getMetaData();   }   catch (Exception e){   e.printStackTrace();   }   return rmd;   }   }   
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有