I've got an application, where I use a JDBC connection. When I run it from the Eclipse environment, there's no problem with anything, neither with connection.
But, when I pack it into executable jar file, and run it from command line, while calling the line
DriverManager.getConnection(url, user, password);
with correct credentials, the application gets frozen. (With wrong login attempt everything is ok - the SQLServerException is thrown).
I tried to create the jar file with different library choices, but the result is the same.
Can anyone help?
I use sqljdbc4.jar library
Constructor of my Database class:
public Database(String url, String user, String password) throws SQLException, ClassNotFoundException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("forname");
connection = DriverManager.getConnection(url, user, password);
System.out.println("get connection");
}
After correct login no Exception is thrown and "get connection" string is not printed out (only "forname" string is printed)
Are you using Java 6 Update 29? If so Update to Java 6 Update 30 or higher (preferably the latest: Update 32). There was a bug in that Java version which caused the Microsoft SQL Server JDBC driver to hang.
"frozen"? Are you catching an Exception with an empty catch block?
I'll bet the JDBC driver JAR is in the CLASSPATH when you run in Eclipse but not when you run the executable JAR. Add the Class-Path to your JAR manifest and put all the JARs with your executable JAR. That should do it.
Related
I am currently using the below properties to connect to a remotely Mssql server from Java spring boot application:
spring.datasource.url=jdbc:sqlserver://ip\\domain;databaseName=name
spring.datasource.username=abc
spring.datasource.password=def
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
The application runs from both windows and unix servers. I need now to start using the windows authentication instead of the db credentials. This is already configured and supported from MSSQL side. From Java spring boot this is not working. I tried to add to the url: integratedSecurity=true it said it cannot find a dll. I added sqljdbc_auth.dll to java jre bin folder and it didnt work.
I'm working on MS SQL Server 2016, Hibernate 5.3.7.Final and Spring bot 2.2.0. For me, adding this line to properties worked: (without jtds!)
spring.datasource.url=jdbc:servername;databasename=your_db_name;integratedSecurity=true
You may also need these properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
And if you encounter a problem with "no sqljdbc_auth in java.library.path".
You may refer to this answer no sqljdbc_auth.
Try use jtds driver, and in apllication properties use:
app.datasource.youraplication.jdbcUrl=jdbc:jtds:sqlserver://yourBdHost/yourSchemaBd;domain=yourDomain
Download the drivers from:
https://learn.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017
Add the jar to your project Right Click on project - > Build Path -> Configure Build Path - > Libraries -> Add External JARs
Add "integratedSecurity=true" to your connection string and remove username and password
I want to connect to Microsoft SQL server 2012 by using SOAP-UI. I have downloaded JTDS-JDBC jar (jtds-1.3.1 Executable Jar File) and store this SoapUI-PRo-5.0.0\bin\ext folder.
Now while writing groovy code by Data Source Step, which Driver should i select.
How can I confirm that this jar file is loaded successfully when soapui is start up.
Try net.sourceforge.jtds.jdbc.Driver() Did you see the documentation?
You should see a message in the soapui log, like:
INFO:Adding [C:\Program Files\SmartBear\soapUI-...\bin\ext\....jar] to extensions classpath
I am trying to run the guestbook application https://developers.google.com/appengine/docs/java/gettingstarted/creating but getting error:
Error screenshot at the url http://goo.gl/rw0dnR
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)
ERROR: transport error 202: connect failed: Connection timed out
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [../../../src/share/back/debugInit.c:741]
I tried it on Eclipse. In Eclipse I click Run menu and then "Debug As"->"Web application". The error popup says:
Problem Occurred: 'Launching Guestbook' has encountered a
problem.Cannot connect to VM. (Cannot connect to VM
com.sun.jdi.connect.TransportTimeoutException)
Could anyone help me troubleshhot.
The code is:
package guestbook;
import java.io.IOException;
import javax.servlet.http.*;
public class GuestbookServlet extends HttpServlet {
#Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}
The code looks fine and it evidently compiles without error. The problem appears to be rather low down in the run time environment of Eclipse, the AppEngine development environment or the Jetty web application server.
I suggest uninstalling and re-installing Eclipse and the AppEngine SDK, or better still try with another computer.
Is another process listening on port 8888? On Windows use netstat -an at a command prompt to check.
You could try another port number. In Eclipse select Run, Debug Configurations..., your project name, Server. Change from 8888 and try again.
To prove that Eclipse is not the problem, temporarily use a fresh copy in a spare directory. Get it from from Eclipse Downloads, selecting "Eclipse IDE for Java EE Developers" and the appropriate 32 bit or 64 bit version. Add both the Google Plugin for Eclipse and App Engine Java SDK using the Eclipse Help -> Install new software... options, working with https://dl.google.com/eclipse/plugin/4.3 as the available software site.
I am trying to access MS SQL server 2005 from a servlet file. I am using JDBC 4.0 driver.
I have already added the JAR files sqljdbc.jar and sqljdbc4.jar files to my Tomcat /lib folder.
But while running code I am getting an error
HTTP Status 500 - Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
How is this caused and how can I solve it?
My code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school;user=sa;password=123");
PrintWriter pwOut = res.getWriter();
pwOut.println("Connected");
Statement st = conn.createStatement();
String searchCriteria = req.getParameter("txtSearch");
ResultSet rs = st.executeQuery("select * from student");
res.setContentType("text/html");
The error message is pretty clear. Tomcat is using the wrong driver.
You state that you copied sqljdbc.jar and sqljdbc4.jar into the Tomcat lib folder. That is most probably the reason for your problem.
You only need sqljdbc4.jar otherwise Tomcat picks up the wrong one.
Try to delete sqljdbc.jar from the Tomcat lib folder
Here is my Code to connect java to Microsoft sql Server 2012
You only need sqljdbc4.jar that avail on offical Microsoft website. Here is the link:
http://download.microsoft.com/download/0/2/A/02AAE597-3865-456C-AE7F-613F99F850A8/sqljdbc_4.0.2206.100_enu.exe
It contains 2 jar files, and I am trying to use sqljdbc4.jar. This is the code I am using to connect:
package com.Sql.ConnectDB;
import java.sql.*;
public class DbClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
**String url="jdbc:sqlserver://localhost;databaseName=Student";**//important
String user="username";
String pass="password";
**Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");**//important
Connection con=DriverManager.getConnection(url,user,pass);
System.out.println("Conneccted Successfully");
}catch(Exception e){
e.printStackTrace();
}
}
}
So I followed the tutorial on the H2 Documentation page and used the "Connecting to a Database using JDBC" method of connecting to the database. I First added the h2-*.jar file to the Lib Folder (through Netbeans) and used the following to make the connection to my database I previously created.
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection("jdbc:h2:~/" + DatabaseName);
This turned out to work in the IDE environment, however when I attempted to run the application directly from the jar executable I get the following error:
java.lang.ClassNotFoundException: org.h2.Driver ...
this error occurs at the Class.forName() class loader. So I did a little looking around and found that this was a prominent problem. One solution people use was to extract the class Loader from the current Thread as so:
Thread t = Thread.currentThread();
ClassLoader cl = t.getContextClassLoader();
cl.getClass().getClassLoader();
Class toRun = cl.loadClass("org.h2.Driver");
Unfortunately this seems to still result in the same error, so i'm wondering what i'm doing wrong. Should I be doing something to make sure the Driver is in the class path? I have no I idea how if that's the case.
Thanks!
You need to add the h2-*.jar file to the classpath when running the application, for example using
java -cp h2*.jar -jar yourApp.jar