王朝网络
分享
 
 
 

java操作mssql2000的小例子

王朝mssql·作者佚名  2008-05-19
宽屏版  字体: |||超大  

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

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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有