java

位置:IT落伍者 >> java >> 浏览文章

java操作mssql2000的小例子


发布日期:2019年03月14日
 
java操作mssql2000的小例子

主要由三个文件组成一个是主执行文件一个是用户界面文件一个是数据模块文件

用户界面这一块是用的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;

}

}

               

上一篇:基于URL的高层次Java网络编程

下一篇:java监听文件Error listenerStart错误