I created the connection between java and mysql as follows:
package conexao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() {
try {
return DriverManager.getConnection("jdbc:mysql://localhost/projetojava","xxxxxxxxxx","xxxxxxxx");
}
catch(SQLException excecao) {
throw new RuntimeException(excecao);
}
}
}
to test the connection I used this code:
package conexao;
import java.sql.Connection;
import java.sql.SQLException;
public class TestaConexao {
public static void main(String[] args) throws SQLException {
Connection connection = new ConnectionFactory().getConnection();
System.out.println("Conexão aberta!");
connection.close();
}
}
But I always get this error:
In-place deployment at \192.168.1.70\Server\html\Pouco Comum\build\web GlassFish Server 4.1.1, deploy, null, false
\\192.168.1.70\Server\html\Pouco Comum\nbproject\build-impl.xml:1048: The module has not been deployed.
See the server log for details. BUILD FAILED (total time: 0 seconds)
How can I solve?
MySQL port seems to be missing in JDBC connection URL. The default port is 3306.
Try below code:
return DriverManager.getConnection("jdbc:mysql://localhost:3306/projetojava","xxxxxxxxxx","xxxxxxxx");
Related
enter image description here
As I have import all required JAR file as shown in above image.
But still it gives me error while trying to connect hive2 via java program.
Error gives me on this line.
Connection connect = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Error:
18/05/30 17:12:15 INFO jdbc.Utils: Supplied authorities: localhost:10000
18/05/30 17:12:15 INFO jdbc.Utils: Resolved authority: localhost:10000
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hive.common.auth.HiveAuthUtils.getSocketTransport(Ljava/lang/String;II)Lorg/apache/thrift/transport/TTransport;
at org.apache.hive.jdbc.HiveConnection.createUnderlyingTransport(HiveConnection.java:519)
at org.apache.hive.jdbc.HiveConnection.createBinaryTransport(HiveConnection.java:539)
at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:309)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:196)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Hive_java.main(Hive_java.java:15)
JAVA Code:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class Hive_java{
private static String driver = "org.apache.hive.jdbc.HiveDriver"; //driver used for hiveserver2
public static void main(String[] args) throws SQLException {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection connect = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", ""); //connecting to default database
Statement state = connect.createStatement();
System.out.println("!!!!!!!!!!Running 1st query!!!!!!!!!!");
System.out.println("Listing tables in hive");
ResultSet show_tables = state.executeQuery("show tables");
while (show_tables.next()) {
System.out.println(show_tables.getString(1));
}
System.out.println("!!!!!!!!!!Running 2nd query!!!!!!!!!!");
System.out.println("Describing slave3_tbl table");
ResultSet describe_table = state.executeQuery("describe slave3_tbl");
while (describe_table.next()) {
System.out.println(describe_table.getString(1) + "\t" + describe_table.getString(2));
}
}
}
This is the actual error I'm getting:
NoInitialContextException: Need to specify class name in environment or system property
I'm trying to have a datasource connection working on a project without servlet but just a main to launch.
This is the main class:
package pacchetto;
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class testIt {
public static void main(String[] args) throws SQLException, NamingException {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
Connection con = ds.getConnection();
try {
con = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null)
System.out.println("Connessione riuscita");
}
}
}
In the same project I also have a servlet file and using it everything works fine. This led me to thinking that the problem must be in this class and not in the servlet.xml or any other configuration file. Should I be wrong and should you need the whole scene I'll be happy to paste them here along with the main.
What am I doing wrong?
I am creating a java console application that connects to a MySQL database. I have outlined below what I've done but the connection is not being established:
Things I have done:
1) I have added the mysql-connector-java-5.1.36.jar to my project bulid path.
2) I have written the code that tries to establish a connection
Can anyone suggest what I'm doing wrong?
The code is given below:
import java.sql.Connection;
import java.sql.DriverManager;
public class main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:1234/first","root","");
System.out.println("Success fully Connected");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
I have a Java application and i need it to connect with my MySQL database's SQL script using JDBC.
Here is my Java application:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package basic;
import basic.ScriptRunner;
import javax.swing.*;
import java.sql.*;
import java.io.*;
/**
*
* #author User
*/
public class javaconnect {
Connection conn = null;
public static Connection ConnectDb(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/new","login","pass");
/*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("D://Java Lenti/EkonomiSoftware/src/basic/new.sql")));
return conn ;
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
return null;}
}
}
The problem is that the Java application connects through MySQL Server, not through the SQL Script. I think the problem is at Connection parameters I gave. Can anyone guide me how to change the connection to make it connect to the SQL script not to the server?
It worked for me:
package com.spring.sample.controller;
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager
.getConnection(
"",
"", "");
ScriptRunner runner = new ScriptRunner(conn, false, false);
runner.runScript(new BufferedReader(new FileReader("new.sql")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
new.sql
insert into hello(name) values ('test');
May be there're some mistakes at your sql file.
i am brand new to Java . Here i am trying to connect to mysql database here my code:
package Services;
package Services;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class LoginService {
private static final String USERNAME="root";
private static final String PASSWORD="";
private static final String CONNECTION_STRING="jdbc:mysql://localhost/testdb";
public void Authenticate(String uname,String password){
Connection connection=null;
Statement statement=null;
ResultSet result =null;
try{
//Class.forName("com.mysql.jdbc.Driver").newInstance();//if java 6 or higher there is no need to load cass
connection =(Connection)DriverManager.getConnection(CONNECTION_STRING,USERNAME,PASSWORD);
System.out.println("Connection to the database is established ");
}catch(SQLException e){
System.out.println("Can't connect to db:" +e );
}
}
}
This end up with an error like this:
Can't connect to db:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/testdb
i have added the jar file and by right clicking in it added the same to build path;what wrong with my code .
You have to download the JDBC driver ..
after that ... add it to your library..
You can read this! It's really simple after you get the idea!
here is the link! http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html
And this one! http://www.mkyong.com/jdbc/how-to-connect-to-mysql-with-jdbc-driver-java/
Good luck!