Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Database/JDBC/SQL/JDO/Hibernate
打印话题 寄给朋友 订阅主题 |
作者 | (原创)小词霸中的数据库连接问题! |
wanghang110119
发贴: 47 积分: 0 |
于 2005-05-19 21:05
我的词霸--源代码: import java.awt.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.event.*; import java.awt.event.*; import java.io.*; import java.sql.*; /*构造词霸图形界面类*/ class WordGraphics { JFrame frame=null; JPanel panel1=null; JPanel panel2=null; JTextField textfield=null; JButton button=null; JLabel label1=null; JLabel label2=null; List list=null; JTextArea textarea=null; //构造函数 public WordGraphics() { ActionListener printListener=new ActionListener() { public void actionPerformed(ActionEvent event) { String root=textfield.getText(); Operation op=new Operation(); String translates=op.getTranslate(root); textarea.insert(translates,0); } }; frame=new JFrame("我的词霸"); Container contentPane=frame.getContentPane(); textfield=new JTextField(" ",20); button=new JButton("查询"); button.setFont(new Font("查询",0,15)); button.addActionListener(printListener); label1=new JLabel("欢迎使用",0); label1.setFont(new Font("欢迎使用",0,30)); label1.setBackground(Color.cyan); label2=new JLabel("输入",0); label2.setFont(new Font("输入",0,15)); list=new List(30,false); String[] word={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; for(int i=0;i<word.length;i++) list.add(word[i]); list.setFont(new Font("word[i]",0,15)); list.setBackground(Color.blue); textarea=new JTextArea(30,50); panel1=new JPanel(); panel1.setLayout(new BorderLayout()); panel1.add(label2,BorderLayout.WEST); panel1.add(textfield,BorderLayout.CENTER); panel1.add(button,BorderLayout.EAST); panel2=new JPanel(); panel2.setLayout(new BorderLayout()); panel2.add(list,BorderLayout.WEST); panel2.add(textarea,BorderLayout.CENTER); contentPane.add(panel1,BorderLayout.NORTH); contentPane.add(label1,BorderLayout.CENTER); contentPane.add(panel2,BorderLayout.SOUTH); frame.setSize(500,300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } /*连接数据库类*/ class ConnectDB { private Connection connection; private String jdbcDriverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String jdbcURL="jdbc:microsoft:sqlserver://127.0.0.1/Worder"; //连接SQL 2000中的Worder数据库 public ConnectDB() { try { Class.forName(jdbcDriverClassName); connection=DriverManager.getConnection(jdbcURL); } catch(SQLException e) { e.printStackTrace(); } catch(ClassNotFoundException cnfe) { cnfe.printStackTrace(); } } //获取数据库连接 public Connection getConnection() { return connection; } } /*提供访问数据库的方法类*/ class Operation { Connection connection; Statement statement; //构造函数,获取数据库连接及创建Statement对象 public Operation() { try { connection=(new ConnectDB()).getConnection(); statement=connection.createStatement(); } catch(SQLException e) { e.printStackTrace(); } } //实现查询功能 public String getTranslate(String words) { try { String sql="select translate from Worder where word=words"; ResultSet rs=statement.executeQuery(sql); rs.next(); return rs.getString(2); } catch(SQLException e) { e.printStackTrace(); return "error"; } } } /*主类*/ public class Worder { public static void main(String args[]) { new WordGraphics(); } } java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket. at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source) at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source) at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source) at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source) at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source) at java.sql.DriverManager.getConnection(DriverManager.java:512) at java.sql.DriverManager.getConnection(DriverManager.java:193) at ConnectDB.<init>(Worder.java:89) at Operation.<init>(Worder.java:124) at WordGraphics$1.actionPerformed(Worder.java:30) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245) at java.awt.Component.processMouseEvent(Component.java:5100) at java.awt.Component.processEvent(Component.java:4897) at java.awt.Container.processEvent(Container.java:1569) at java.awt.Component.dispatchEventImpl(Component.java:3615) at java.awt.Container.dispatchEventImpl(Container.java:1627) at java.awt.Component.dispatchEvent(Component.java:3477) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128) at java.awt.Container.dispatchEventImpl(Container.java:1613) at java.awt.Window.dispatchEventImpl(Window.java:1606) at java.awt.Component.dispatchEvent(Component.java:3477) at java.awt.EventQueue.dispatchEvent(EventQueue.java:456) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137) at java.awt.EventDispatchThread.run(EventDispatchThread.java:100) java.lang.NullPointerException at Operation.<init>(Worder.java:125) at WordGraphics$1.actionPerformed(Worder.java:30) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245) at java.awt.Component.processMouseEvent(Component.java:5100) at java.awt.Component.processEvent(Component.java:4897) at java.awt.Container.processEvent(Container.java:1569) at java.awt.Component.dispatchEventImpl(Component.java:3615) at java.awt.Container.dispatchEventImpl(Container.java:1627) at java.awt.Component.dispatchEvent(Component.java:3477) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128) at java.awt.Container.dispatchEventImpl(Container.java:1613) at java.awt.Window.dispatchEventImpl(Window.java:1606) at java.awt.Component.dispatchEvent(Component.java:3477) at java.awt.EventQueue.dispatchEvent(EventQueue.java:456) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137) at java.awt.EventDispatchThread.run(EventDispatchThread.java:100) 我的初衷是编写一个像金山词霸那样的小软件 但是编译通过,运行之后就出现上面所列的那些错误, 我自己初步看了看,感觉是不是数据库的路径设置有误? 我的环境变量是: classpath: .;C:\j2sdk1.4.2_02\lib\dt.jar;C:\j2sdk1.4.2_02\lib\tools.jar;.;C:\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;C:\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;C:\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar path: c:\j2sdk1.4.2_02\bin 或者是有别的错误,可能程序很长,看起来很费劲, 但请大家耐心帮我看看,指教一下,先谢啦! 只有去做才知道行不行! CJSDNer6月14日北京聚会花絮 |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
13288 | (原创)小词霸中的数据库连接问题! | wanghang110119 | 8042 | 2005-05-19 21:05 |
10994 | Re:(原创)小词霸中的数据库连接问题! | why | 249 | 2005-05-19 22:54 |
11628 | Re:(原创)小词霸中的数据库连接问题! | wanghang110119 | 818 | 2005-05-20 13:05 |
10946 | Re:(原创)小词霸中的数据库连接问题! | why | 150 | 2005-05-21 13:04 |
11616 | Re:(原创)小词霸中的数据库连接问题! | wanghang110119 | 751 | 2005-05-21 17:12 |
11575 | Re:(原创)小词霸中的数据库连接问题! | wanghang110119 | 88 | 2005-05-21 17:20 |
10873 | Re:(原创)小词霸中的数据库连接问题! | why | 604 | 2005-05-21 22:24 |
11597 | Re:(原创)小拾灾械氖?菘饬?游侍猓?></td></tr><tr class= | wanghang110119 | 269 | 2005-05-22 10:07 |
11036 | Re:(原创)小词霸中的数据库连接问题! | brightman | 23 | 2005-05-23 16:36 |
11392 | Re:(原创)小词霸中的数据库连接问题! | wanghang110119 | 61 | 2005-05-23 20:34 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |