SQL Server 2008 с сервлетом - PullRequest
0 голосов
/ 15 июля 2011

Я пытаюсь подключить SQL Server 2008 с сервлетом. Это мой код.

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);
}

}

Я получаю следующую ошибку

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.

1 Ответ

1 голос
/ 16 июля 2011

При загрузке драйвера JDBC сервера Sql с сайта Microsoft.

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

Распакуйте его, и вы найдете два файла jar.

sqljdbc.jar - используйте этот драйвер с версией Java до 1.6 sqljdbc4.jar - используйте этот драйвер с версией Java 1.6

Вы получаете сообщение об ошибке, потому что ваш проект скомпилирован с Java 1.6, но вы используете sqljdbc.jar вместо sqljdbc4.jar

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...