Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Query problems from Tomcat servlets to MSSQL Server Express 2012
- static {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private final static String host = "ip-address:1433";
- private final static String database = "databasename";
- private final static String user = "user";
- private final static String passwd = "password";
- public static void main(String[] args) {
- try {
- String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";integratedSecurity=true;";
- Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
- PreparedStatement pstm = con.prepareStatement("select * from sektor");
- ResultSet res = pstm.executeQuery();
- while(res.next())
- System.out.println("Sektor: " +res.getString("sektornavn"));
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- static {
- try {
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private final static String host = "ip-address:1433";
- private final static String database = "databasename";
- private final static String user = "user";
- private final static String passwd = "password";
- @Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- resp.setContentType("text/plain");
- resp.setCharacterEncoding("UTF-8");
- // Write response
- PrintWriter writer = resp.getWriter();
- try {
- String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";";
- writer.println("Connecting<br/>");
- Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
- writer.println("Preparing statement<br/>");
- PreparedStatement pstm = con.prepareStatement("select * from sektor");
- writer.println("Executing statement<br/>");
- ResultSet res = pstm.executeQuery();
- while(res.next())
- writer.println("Sektor: " +res.getString("sektornavn")+"<br/>");
- } catch (SQLException e) {
- writer.println(e.getMessage());
- }
- writer.flush();
- writer.close();
- }
Add Comment
Please, Sign In to add comment