My application is building successfully but won't run the query. Here's what I've done so far:
Downloaded JDBC Driver and used to successfully create connection to database, which is showing in services tab
Added CLASSPATH variable and added C:\Program Files\Java\jre1.8.0_221\bin;C:\Program Files\Java\jdk1.8.0_221\bin;.;C:\Program Files\Java\Microsoft JDBC Driver 7.4 for SQL Server\sqljdbc_7.4\enu\mssql-jdbc-7.4.1.jre8.jar as well as my JRE and JDK bin folders and .
Run below code
import java.sql.*;
public class TestCode {
public static void main(String[] args)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://mysqlserver:1433;databaseName=mydatabase;integratedSecurity=false;";
Connection conn = DriverManager.getConnection(url, "username", "password") ;
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT TOP 10 UserID FROM dbo.Users");
while (rs.next())
{
String userID = rs.getString("UserID");
System.out.println(userID);
}
}
catch (Exception e)
{
System.err.println("Error");
System.err.println(e.getMessage());
}
}
}
The error I'm getting is:
Error
com.microsoft.sqlserver.jdbc.SQLServerDriver
Which is the just the name of my driver. I got this from right clicking on the driver, clicking customize and copying the Driver Class from this Window.
Fixed.
Needed to add JDBC driver file to project library
Related
I am trying to get data from a SQLite Db into my Servlet.
But I keep getting the Error: No suitable driver found for jdbc:sqlite:C://Users/Abc/Documents/sqlite/test.db
I am using Eclipse, Windows and Tomcat.
My source code looks like:
public class Servlet extends HttpServlet {
...
private void SQLite_Test() {
try {
String url = "jdbc:sqlite:C://Users/Abc/Documents/sqlite/test.db";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs;
rs = stmt.executeQuery("SELECT * FROM first");
Class.forName("org.sqlite.JDBC");
while ( rs.next() ) {
System.out.println(rs.getInt("id") + "\t"+ rs.getString("name"));
}
conn.close();
}catch..}
protected void doGet(...
{
SQLite_Test();
...}
..}
I added the sqlite-jdbc-3.27.2.1 jar into the Classpath / Referenced Libraries and into Resources.
I tried it in a Java Project and it worked without problems.
Thanks for your help in advance.
I forgot to put the driver JAR into web-inf/lib.
I am trying to connect to hive server using java.
I am runnig the code in Eclipse Oxygen and has Java 8 installed.
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException
{
try
{
Class.forName(driverName);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://<IP>:port/database", "username", "password");
Statement stmt = con.createStatement();
String sql = "show tables";
stmt.executeQuery(sql);
}
These are the external libraries I am using.
commons-logging-1.2
curator-client-2.0.0-incubating
hadoop-common-3.1.0
hive-exec-3.0.0
hive-jdbc-3.0.0
hive-metastore-3.0.0
hive-service-3.0.0
hive-service-rpc-2.1.0
httpclient-4.5.6
httpcore-4.4.10
libfb303-0.9.3
libthrift-0.9.3
log4j-1.2.17
slf4j-api-1.8.0-beta2
When I run the code I am getting the following error.
java.lang.NoSuchMethodError: org.apache.hive.service.auth.HiveAuthFactory.getSocketTransport(Ljava/lang/String;II)Lorg/apache/thrift/transport/TTransport
I can't figure out what is causing the error. I tried different versions of jars. Am I missing any library? Please help me.
I have developed a REST application using jersey where to connect to database(mysql) I use jdbc connection. Following is the configuration.
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/DBNAME";
static final String USER = "root";
static final String PASS = "********";
public List<Item> getAll() {
List<Item> results = new ArrayList<>();
try (Connection conn = (Connection) DriverManager.getConnection(DB_URL,
USER, PASS);
Statement stmt = (Statement) conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM items");) {
while (rs.next()) {
// Retrieve by column name
System.out.println(rs.getInt("ID"));
System.out.println(rs.getString("FormerCode"));
System.out.println(rs.getString("NewCode"));
results.add(new Item(rs.getInt("ID"), rs
.getString("FormerCode"), rs.getString("NewCode")));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return results;
}
All things works when I deploy via Eclipse. But when I create the war file and deploy it manually on tomcat server it gets the exception saying there is no suitable driver.
No suitable driver found for 'jdbc:mysql://localhost:3306/DBNAME
I have included the mysql-connector jar files in both WEB-INF/lib folder and TOMCAT_HOME/lib folder. But I am still getting this error. Server is running on Ubuntu Server 14.04.
Paths where jar is added.
src/main/webapp/WEB-INF/lib
usr/share/tomcat7/lib
What seems to be the problem hence I have added the jar files to necessary locations??
Sometimes java class failed to pick driver's(mysql java connector jar file) path from lib folder. To remove this issue you can extract connector jar in following directory of your project.
src/main/webapp/WEB-INF/classes
OR
You may also compile the java file with classpath reference of mysql_java_connector.jar.
The answer was when using a DriverManager interface to create a JDBC Connection, always create an instance of your JDBC driver first in order to load it into your Classloader.
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
Thanks all.
I am following a tutorial on connecting my SQLite database to a Java application.
When I run the program I get the following error in the console of NetBeans:
run:
Error connecting to the databasejava.sql.SQLException: No suitable
driver found for jdbc:C:\Users\lawman\Documents\Java Working
Directory\LoginSql\src\project123.sqlite
BUILD SUCCESSFUL (total time: 0 seconds)
Here is my directory:
I have code to connect to the database in class tobecalledbymain.
I have main in mainclass which creates an instance of tobecalledbymain.
In my library file, I have sqlite-jdbcs.jar imported.
Here is the code for tobecalledinmain:
import java.sql.*;
public class tobecalledinmain {
public tobecalledinmain(){
Connection con = null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
+ "Java Working Directory\\LoginSql\\"
+ "src\\project123.sqlite");
st=con.createStatement();
//select all records from the table employee
//table has three firlds: employeeid,name and surname
rs=st.executeQuery("SELECT * FROM Employee;");
while (rs.next())
{
int id = rs.getInt("Employeeid");
String name = rs.getString("Name");
System.out.println("id = " + id);
System.out.println("name= " + name);
System.out.println();
}
rs.close();
st.close();
con.close();
}catch(Exception e)
{
System.out.println("Error connecting to the database" + e);
}
}
}
Here is the mainclass code:
public class mainClass {
public static void main(String[] args){
new tobecalledinmain();
}
}
;;
I am not sure why we need to semi-colons!
Anyway, when the tutorial concludes he gets a result from the console. I get the said error message.
What are the drivers in the error message referring to and how do I get them?
Your jdbc connection string does not specify sqlite. Try this, and use forward slashes.
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");
This was the WRONG answer, pls disregard.
You need to add the .jar for the SQLite database driver to your classpath when you run your code. See https://bitbucket.org/xerial/sqlite-jdbc
You can see how to do that in Netbeans here: How to add a JAR in NetBeans
I have the following issue with my code :
import java.sql.*;
public class App {
public static void main(String[] args){
String url = "jdbc:mysql://localhost:3306" ;
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{ System.out.println("Eroare incarcare driver!\n" + e);
return;
}
try{
Connection con = DriverManager.getConnection(url);
// Golim tabelul persoane
String sql = "DELETE FROM persoane";
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
stmt.execute("CREATE DATABASE IF NOT EXISTS test");
stmt.execute("USE test");
i get the exception...any idea how i can make this work? thx.
enter code here
You need to download and add the jdbc connector to your classpath.
http://dev.mysql.com/downloads/connector/j/
java.lang.ClassNotFoundException occured due to "class not found" in your project/war/ear. Exception is very self explanatory, How to solve it.
In your case:
Add com.mysql.jdbc.Driver driver class/jar in your build/deployment/lib path you can
download it HERE
Read here
Offical
Make sure you have the MySQL driver on your application classpath.
Change
Connection con = DriverManager.getConnection(url);
to
Connection con = DriverManager.getConnection(url,"username","password");
and replace it with yourusername and password