Sql Server连接数据库失败

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42189604/article/details/81330837

sql server连接数据库失败,我们可以从以下几个方面去分析问题:

1.数据库服务是否打开。

2.数据库连接配置是否正确。

3.账号密码是否正确。2,3点都可以本地登录验证一下。

4.项目是否导入了jdbc相关的jar包。

以上都排除过后,就需要分析其他的原因了。在此记一下我的一个经历。有次启动项目包数据库连接失败的错,但是我昨天才登录成功了,什么都没动过,今天突然就不行,把以上原因都检查了一遍发现没问题,后面就想是不是tomcat有问题了,后面把项目全部移除后还是报这个错,就确定了是tomcat的问题的。在webapps里面有残留的项目,后面把它删掉了,也把tomcat下temp和log目录清干净了,相当于清缓存,后面再运行,顺利启动。

 

展开阅读全文

连接数据库 ms sql server 失败

08-02

class connectMSSQLSERVERrnrn public void connect()rn rnrnrn //Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();rn// connect to the databasern // sqlConn=java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.2:1433;DatabaseName=pubs;SelectMethod=direct","dxxx","");rnrnrn String dburl = "jdbc:microsoft:sqlserver://192.168.1.2:1433;DatabaseName=hd_pub";rn String user = "sa";rn String password = "sa";rn tryrn rn Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");rn Connection c = DriverManager.getConnection(dburl,user, password);rnrn if(c.isClosed())rn rn System.out.println("error");rn return;rn rnrn c.setAutoCommit(false);rn Statement s = c.createStatement();rn ResultSet r = s.executeQuery("select cempno,cpdm from sys_parm");rnrn while(r.next())rn rn System.out.println(r.getString(1) + r.getString("cpdm"));rn rn s.close();rn c.close();rn rn catch(Exception e)rn rn e.printStackTrace();rn rn rnrnrn错误提示:rnjava.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriverrnrn at java.net.URLClassLoader$1.run(URLClassLoader.java:198)rnrn at java.security.AccessController.doPrivileged(Native Method)rnrn at java.net.URLClassLoader.findClass(URLClassLoader.java:186)rnrn at java.lang.ClassLoader.loadClass(ClassLoader.java:299)rnrn at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)rnrn at java.lang.ClassLoader.loadClass(ClassLoader.java:255)rnrn at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)rnrn at java.lang.Class.forName0(Native Method)rnrn at java.lang.Class.forName(Class.java:140)rnrn at aaa.connectMSSQLSERVER.connect(Frame1.java:133)rnrn at aaa.Frame1.jButton1_actionPerformed(Frame1.java:65)rnrn at aaa.Frame1_jButton1_actionAdapter.actionPerformed(Frame1.java:86)rnrn at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1764)rnrn at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1817)rnrn at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:419)rnrn at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:257)rnrn at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)rnrn at java.awt.Component.processMouseEvent(Component.java:5134)rnrn at java.awt.Component.processEvent(Component.java:4931)rnrn at java.awt.Container.processEvent(Container.java:1566)rnrn at java.awt.Component.dispatchEventImpl(Component.java:3639)rnrn at java.awt.Container.dispatchEventImpl(Container.java:1623)rnrn at java.awt.Component.dispatchEvent(Component.java:3480)rnrn at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3450)rnrn at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3165)rnrn at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3095)rnrn at java.awt.Container.dispatchEventImpl(Container.java:1609)rnrn at java.awt.Window.dispatchEventImpl(Window.java:1590)rnrn at java.awt.Component.dispatchEvent(Component.java:3480)rnrn at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)rnrn at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)rnrn at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)rnrn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)rnrn at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)rnrn at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)rnrnrn1. 我使用 jdatastore explorer 可以连接成功,连接参数也是那样 copy 下来的rn实在想不出来那里还有错误, jsdv 的驱动安装了,三个文件也 copy 到了 jdk 下面的 lib 下面rnrn2. 我怎么知道一个 connection 对象的状态,是否连接,难道一定要执行一个 SQL 吗???rnrn 论坛

没有更多推荐了,返回首页