简体   繁体   中英

Sql server 2008 with servlet

I am trying to connect the sql server 2008 with servlet.this is my code.

public class ProcessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public ProcessServlet() {
    super();
    // TODO Auto-generated constructor stub
}
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
          // Declare the JDBC objects.
          Connection con = null;              
          ResultSet rs = null;
          try {
          // Establish the connection. 
          SQLServerDataSource ds = new SQLServerDataSource();
          ds.setUser("sa");
          ds.setPassword("password123");
          ds.setServerName("ENMEDIA-EA6278E\\ENMEDIA");
          ds.setDatabaseName("DishTV_Voting");              
          con = ds.getConnection();              
                  // Execute a stored procedure that returns some data.
          Statement stmt = con.createStatement();
          /*  stmt.executeUpdate("CREATE DATABASE  hello");
          stmt.executeUpdate("Use hello");
          String table = 
          "CREATE TABLE Employee11(Emp_code integer, Emp_name varchar(10))";
          stmt.executeUpdate(table);
          System.out.println("Table creation process successfully!");*/
          rs = stmt.executeQuery("SELECT question_text FROM 
                       otvtbl_question WHERE question_id = 10");
             while ( rs.next() ) {
                 String lastName = rs.getString("question_text");
                 System.out.println(lastName);
             }
            con.close();
         } catch (Exception e) {
             System.err.println("Got an exception! ");
             System.err.println(e.getMessage());
         }
     }
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request,response);
}

}

I am getting the following error

Jul 15, 2011 6:56:04 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SEVERE: Java Runtime Environment (JRE) version 1.6 is not supported by this driver.     
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Got an exception! 
Java Runtime Environment (JRE) version 1.6 is not supported by this driver. 
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.

When you download Sql server JDBC driver from Microsoft site.

http://www.microsoft.com/download/en/details.aspx?id=21599

Unpack it and you will find two jar files.

sqljdbc.jar - use this driver with java version prior 1.6 sqljdbc4.jar - use this driver with java version 1.6

You are getting the error message because your project is compiled with java 1.6 but you are using sqljdbc.jar instead of sqljdbc4.jar

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM