将SQL Server数据库连接到Java

时间:2014-12-18 15:42:16

标签: java sql-server jsp

我的项目没有使用任何IDE。 整个编码使用Textpad完成。 所以我需要帮助连接sql server数据库和我的项目。 这是bean类,其中编写了用于数据库连接的业务逻辑。 我已经安装了SQL服务器并使用Windows身份验证创建了一个名为“vt”的数据库。 我甚至为数据库创建了dsn,即vtdsn。但仍然没有找到课程的例外。 请帮忙。 M卡在这里。 之前它在我使用MS Access进行数据库时工作正常。 但是在使用SQL服务器进行数据库连接时我需要做些什么改变

package  ankit;

import  java.sql.*;

public class RegisterBean {

    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    private String myfirstname, mylastname, myemail, mypassword, dateofbirth, gender, college, address;

    //code for setter and getter method. removed to shorten the code//
    public RegisterBean() {
        System.out.println("Default constructor  of RegisterBean is called");
    }

    public void makeConnection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:odbc:vtdsn");
        } catch (Exception e) {
            System.out.println("Error in making connection ");
            e.printStackTrace();
        }
    }

    public boolean checkRegistration(String fN, String lN, String email, String pwd, String dob, String gender, String coll, String addr) {
        boolean flag;
        makeConnection();

        try {
            pst = con.prepareStatement("insert into studentdetail values(?,?,?,?,?,?,?,?)");
            pst.setString(1, fN);
            pst.setString(2, lN);
            pst.setString(3, email);
            pst.setString(4, pwd);
            pst.setString(5, dob);
            pst.setString(6, gender);
            pst.setString(7, coll);
            pst.setString(8, addr);

            int i = pst.executeUpdate();
            System.out.println(i);

            if (i >= 1) {
                flag = true;
            } else {
                flag = false;
            }
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }

        return flag;
    }
}

将驱动程序添加到我的java bean类文件所在的Tomcat的classes文件夹中 并将驱动程序添加到jre / lib和jre / lib / ext文件夹中。 现在收到此错误

Error in making connection
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost/S
QLEXPRESS/Databases/vt:1433;
        at java.sql.DriverManager.getConnection(DriverManager.java:604)
        at java.sql.DriverManager.getConnection(DriverManager.java:243)
        at ankit.RegisterBean.makeConnection(RegisterBean.java:113)
        at ankit.RegisterBean.checkRegistration(RegisterBean.java:124)
        at org.apache.jsp.components.jsp.RegisterDataBase_jsp._jspService(Regist
erDataBase_jsp.java:117)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
90)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:123)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp
11Processor.java:1023)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoin
t.java:1852)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:603)
        at java.lang.Thread.run(Thread.java:722)
java.lang.NullPointerException
        at ankit.RegisterBean.checkRegistration(RegisterBean.java:127)
        at org.apache.jsp.components.jsp.RegisterDataBase_jsp._jspService(Regist
erDataBase_jsp.java:117)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper
.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:3
90)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:123)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp
11Processor.java:1023)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoin
t.java:1852)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.
java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor
.java:603)
        at java.lang.Thread.run(Thread.java:722)
false

解决: 解决我的问题。

我所做的只是下载sqljdbc4驱动程序并将驱动程序粘贴到TOMCAT lib文件夹中。 (注意:下载驱动程序后将其解压缩到某处,然后你会在该解压缩文件夹中找到原始驱动程序。将该文件粘贴到tomcat lib文件夹中。这就是我做错了,我只是粘贴了下载的rar文件互联网,这就是为什么tomcat无法找到驱动程序)。 粘贴lib文件夹中的驱动程序后,只需将类路径设置为该驱动程序即可完成。

2 个答案:

答案 0 :(得分:0)

您必须为SQLServer导入驱动程序jar文件,然后编辑如下代码:

public void makeConnection() {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        con = DriverManager.getConnection("jdbc:sqlserver://localhost:3306/DBName", "DBUsername", "DBPassword");
    } catch (Exception e) {
        System.out.println("Error in making connection ");
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

SQLException: No suitable driver found

您需要放置适当的驱动程序。

创建一个lib目录,将所有jar放在那里(特别是jdbc连接器http://dev.mysql.com/downloads/connector/j/

然后将其添加到您的课程路径:http://www.ibm.com/developerworks/library/j-classpath-windows/祝你好运

取自此主题:ClassNotFoundException for com.mysql.jdbc.Driver -not using an IDE