这是连接文件[code=Java][/code]package com.zzx.util;
import java.sql.*;
public class DataConn {
public static void main(String[] srg) {
new DataConn().getConn();
//System.out.println("conn successfully");
}
/*
* mysql数据库驱动
* */
// final String driver = "com.mysql.jdbc.Driver";
// final String url = "jdbc:mysql://localhost:3306/jpkc";
// final String user = "root";
// final String password = "root";
//sql server 2000的数据驱动
final String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; // 加载server2000 JDBC驱动
final String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=jpkc"; // 连接服务器和数据库
final String user = "sa";
final String password = "sa";
// final String driver ="net.sourceforge.jtds.jdbc.Driver";
// final String url = "jdbc:jtds:sqlserver://localhost:1433/jpkc";
// final String user = "sa";
// final String password = "123456";
static Connection conn = null;
public DataConn() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("驱动类加载失败!");
}
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
public Connection getConn() {
return conn;
}
public void closeConn() {
try {
conn.close();
} catch (SQLException e) {
System.out.println("和数据库断开连接失败!");
}
}
}
我用myeclipse建立数据库连接的时候就是按这个来的(我用的是sql2000).在myeclypse加载工程文件,在地址栏可以看到首页的内容,但是点下相关的内容就有错误了,老师说的数据库连接的问题。这时我发现myeclpise显示用户登录‘sa’失败等信息。我的数据库登录名和密码都是sa.错误信息如下用户 'sa' 登录失败。 at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2816)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2254)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:602)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:344)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:182)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.zzx.util.DataConn.<init>(DataConn.java:43)
at com.zzx.manager.NewsManager.queryCount(NewsManager.java:108)
at org.apache.jsp.jiaocai_jsp._jspService(jiaocai_jsp.java:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)