I have a SQLServer database named : EventConsumerDB.
I have created Login, Password and User for this Login :
CREATE LOGIN EventConsumer WITH PASSWORD = 'EventConsumer', DEFAULT_DATABASE = EventConsumerDB;
CREATE USER EventConsumer FOR LOGIN EventConsumer;
Granted ROLE is : db_accessadmin
From Eclipse, I try to create the connection to this DataBase :
URL : jdbc:sqlserver://MyPC:1433;DATABASE_NAME=EventConsumerDB
Username : EventConsumer
Password : EventConsumer
Drive classname = com.microsoft.sqlserver.jdbc.SQLServerDriver
The result is always the same :
It's the first time I install a SQL Server at home, it's quiet complicated...
Is there anything I've forgotten ?
Thanx by advance.
Have you tried this: Connection-URL - Example ?
It shows you how to connect to an sql server via an URL:
public class ConnectURL {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=<password>";
try (Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();) {
String SQL = "SELECT TOP 10 * FROM Person.Contact";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
OK, thanx to those who've tried to help me.
The problem was the following : When I installed the product (Windows 10 platform), Windows Authentication was the default authentication mode.
Solution found on MS page : "Change Server Authentication Mode"
In the SSMS, right-click on the server name > properties > security.
Then in the "server authentication" block, replace Windows Authentication Mode by SQL Server and Windows Authentication Mode.
Restart the server.
Note : don't forget to activate the TCP-IP protocol if needed. To turn it ON, you'll have to use the SQL Server Configuration console or PowerShell
Related
I have SQL developer installed and a properly configured DB, I create a user from sys like so:
CREATE USER random IDENTIFIED BY 12345;
GRANT ALL PRIVILEGES TO random;
I attempt to connect to the oracle SQL database with the ojdbc8.jar found in oracles website like this:
String url = "jdbc:oracle:thin:random/12345#localhost:1521:home";
try{
Connection dbConn = DriverManager.getConnection(url);
}catch(Exception e){
System.out.println("Exception: " + e.getLocalizedMessage());
}
However I receive this error:
Exception: ORA-01017: invalid username/password; logon denied
The last time I asked this question it was just populated by answers that have no actual answer to the problem at hand, I don't need to change the driver to a different one, I don't need to instantiate some sort of factory nonsense that just adds complexity, all I want to know is how I'm meant to connect to an account that is part of my DB so I can perform basic SQL functions.
Edit:
It just occurred to me that it is a pdb, is there a modification needed to the connection url that anyone can point out?
Try:
Connection dbConn = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:home", "random", "12345");
PDB connections need a slight tweak.
Please use localhost:port/sid instead of localhost:port:sid:
if (isPluggableDB) {
conn = DriverManager.getConnection("jdbc:oracle:thin:#" + hostName + ":" + hostPort + "/" + sid, userName, password);
} else {
conn = DriverManager.getConnection("jdbc:oracle:thin:#" + hostName + ":" + hostPort + ":" + sid, userName, password);
}
I am trying to create my first API using java httpServlet and netbeans which will be connected to a database on google cloud based its examples and documentations. I have not created the database, but I was given the necessary credentials and vars to create the connection. So I have downloaded the project from github, and when I tried to open it from netbeans there was some issues concerning dependencies ... So I resolved them, I replaced the credentials with their values and run the project; Unfortunately an error was thrown: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. I made some searches but did not get any result... Could it be an error from this code ? an error from database security if it was invisible on cloud? Any help is more than appreciated.
public class ListTables {
public static void main(String[] args) throws IOException, SQLException {
String instanceConnectionName = "<foo:foo:bar>";
String databaseName = "myDatabaseName ";
String username = "myUsername ";
String password = "<myPass>";
if (instanceConnectionName.equals("<insert_connection_name>")) {
System.err.println("Please update the sample to specify the instance connection name.");
System.exit(1);
}
if (password.equals("<insert_password>")) {
System.err.println("Please update the sample to specify the mysql password.");
System.exit(1);
}
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);
try{
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
/* try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("select * from wf_accounts;");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}*/
}catch(Exception ex){
System.out.println("Error: " + ex.toString());
}
Update
Same error was thrown when I did this:
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
"",
"");
Any hints?
First you have to check whether the mysql server is running on.If you are Windows user you can simply
Go to Task Manager
Go to Services
then search for your Mysql server(eg:for my case its MYSQL56)
then you will see under the status column it says its not running
by right clicking and select start
If it's already running then you have to do as in mysql Documentation,
You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property
autoReconnect=true
to avoid this problem
I'm trying to use Ucanaccess library with java to connect to Microsoft Access ...
So i tried executing this code and it worked ....
String url = "jdbc:ucanaccess:\\C:/Access.ACCDB";
String query = "select * from [Donations]";
try
{
java.sql.Connection con =DriverManager.getConnection(url, "","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println("Account No: " + rs.getString(1));
System.out.println("Surname: " + rs.getString(2));
System.out.println("First Name: " + rs.getString(3));
}
}
catch(SQLException ex)
{
while (ex!=null)
{
System.out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException();
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
let me explain what i'm trying to do
I have a Asp.Net website that uses Microsoft Access Database and in the same time i need a server (Java Application) to access the same Database (Microsoft Access) but when i uploaded the Database online to test if the server can get to the Database and modified the String for the connection ....
here is what it looked like
String url = "jdbc:ucanaccess://www.filetolink.com/download/?h=eaa4aab688d25e270539868d712961ab&t=1467140283&f=3ca9ad1869;";
String query = "select * from [Donations]";
try
{
java.sql.Connection con =DriverManager.getConnection(url, "","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println("Account No: " + rs.getString(1));
System.out.println("Surname: " + rs.getString(2));
System.out.println("First Name: " + rs.getString(3));
}
}
catch(SQLException ex)
{
while (ex!=null)
{
System.out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException();
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
I tried running the code but I had an error message ... Here it is :
SQL Exception: UCAExc:::3.0.6 given file does not exist:
http:\www.filetolink.com\download\?h=e6dc57e3485defa7c53e1c968a87fb1f&t=1467139906&f=3ca9ad1869
So What i'm asking for is :
1.How can I publish the website with the access database and let both of the Server and the website access the Database ?? (The server and the website aren't on the same Machine or computer .... )
2.if the answer to my question is that i can't ... then what can i do ?? other suggestions ? for having Database that can be accessed by a Remote Server (Java Application ) and A website (Asp.Net)
MS Access is a desktop database not a server database management system. If a program wants to access to such a database, the database file has to be on a local or network filesystem. So if your two applications are running on different machines in the same intranet, putting the database file on network filesystem would be a solution.
BUT: MS Access was never meant to be used as a web application's backend database. You should use a real server database like MS SQL Server, PostgreSQL or MySQL instead. Then your applications can access the database via network protocol. Theoretically it is possible to open the database port to the internet so that you can connect from everywhere. But this is not recommended due to security reasons.
So if your two applications aren't running in the same intranet it is better to create some webservices and let your other program call these webservices to get the needed information.
I had problem when connecting to Oracle Cloud Database from java code.
I have no problem connecting other non-cloud oracle databases.
I can connect to the Oracle Cloud Database with sql tools, except from the java codes.
The hostname, username and password is correct one, i don't reveal the real username and password.
Error: java.sql.SQLException:
SQLException: SQLState(null) vendor code(17002)
java.sql.SQLException: Io exception: Oracle Error ORA-12650: No common encryption or data integrity algorithm
My code as following:
String dbURL = "jdbc:oracle:thin:#192.133.133.23:1521:ORCL";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(dbURL, "username1", "password");
}catch(Exception)
{
e.printStacktrace();
}
/**
* The below one is for oracle thin client
Its worked for the ojdbc6 driver.
*/
public Connection conCheck() {
Connection con=null;
try { //step1 load the driver class
Class.forName("oracle.jdbc.OracleDriver");
Properties props = new Properties();
props.put("oracle.net.encryption_client", "REQUIRED");
props.put("oracle.net.encryption_types_client", "( " + AnoServices.ENCRYPTION_AES256 + "," +AnoServices.ENCRYPTION_AES192 + ")");
props.put("oracle.net.crypto_checksum_types_client", "( SHA1 )");
props.put("user", "username");
props.put("password","password");
//step2 create the connection object
con=DriverManager.getConnection( "jdbc:oracle:thin:#host:port:serveiceid",props);
System.out.println("Con"+con);
}catch(Exception e) {e.printStackTrace(); }
return con;
}
Seems like changing syntax in your connection string to lookup SERVICE_NAME instead of SID must help you connect your database.
String dbURL = "jdbc:oracle:thin:#192.133.133.23:1521/ORCL";
Additional Read : Thin-style Service Name Syntax
If this as well doesn't help, then would suggest to add below 2 lines to your sqlnet.ora in database.
SQLNET.CRYPTO_CHECKSUM_TYPES_CLIENT= (SHA1)
SQLNET.CRYPTO_CHECKSUM_CLIENT = requested
First mandatory check is to verify the oracle jdbc version. if you use incompatible version,we will get this kid of errors.
I'm trying to connect to a MySql database from a test-app in Java.
I have just followed a plain tutorial I found online. The code works (and compiles OK), but I cant log in to the db and fetch anything. Getting the error:
1:
Error connecting to DB: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user '******'#'%' to database '*****_javaapp'
java.lang.NullPointerException
Why does Java add the '#', and the '%' symbols in the console? Is that why I cant log in?
I am not able to modify priveliges on the db user. It's just set to "default" I guess by the ISP provider...
I have discussed this '#' & '%' issue with my friend, and I understand that these symbols are MySql syntax for "accept anything", but I still dont know what do to fix it as my MySql knowlede is limited..
If I modify some details in the db connection I sometimes get:
2:
Error connecting to DB: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
java.lang.NullPointerException
My code
My Main class:
package test;
import java.awt.EventQueue;
public class Main {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
// DB CONNECT
DBConnect connect = new DBConnect();
connect.getData();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
My connect class:
package test;
import java.sql.*;
public class DBConnect {
private Connection con;
private Statement st;
private ResultSet rs;
public DBConnect() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://mywebhostadress:3306/some_db_name","some_db_user", "somepassword");
st = con.createStatement();
} catch (Exception ex) {
System.out.println("Error connecting to DB: " + ex);
}
}
public void getData() {
try {
String query = "select * from Client";
rs = st.executeQuery(query);
System.out.println("Records from db: ");
while (rs.next()) {
String org_number = rs.getString("org_number");
String org_name = rs.getString("org_name");
System.out.println("org_number: " + org_number + "org_name: " + org_name);
}
} catch (Exception exgetData) {
System.out.println(exgetData);
}
}
}
Thanks for any helpfull tips!
UPDATE:
Hmmm…
In MySQL Workbench I manage to set up the connection OK… But I get the same error here:
“The account you are currently using does not have sufficient privileges to make changes to MySQL users and privileges.”
Also, while trying to forward engineer in MySQL Workbench:
ERROR 1044: Access denied for user 'some_db_user'#'%' to database 'new_schema'
SQL Statement:
CREATE SCHEMA new_schema
I have also tried to do it directly in phpMyAdmin, but there I can see this error on the front page:
“Create new database - No Privileges”
I have tried to grant my default (and only) user all the rights I can give, but it doesn’t get accepted when I do a query in phpMyAdmin.
I Guess this is why I can’t access it from my Java application. But I might type something wrong…
Shouldn’t it be:
GRANT ALL PRIVILEGES ON some_db_name.* TO 'some_db_user'#'%' WITH GRANT OPTION; ?
(taken from: this post)
I get:
Error
SQL query:
GRANT ALL PRIVILEGES ON some_db_name . * TO 'some_db_user '#'%' WITH GRANT OPTION
MySQL said:
1044 - Access denied for user 'some_db_user '#'%' to database ' some_db_name '
The error you're seeing:
Error connecting to DB:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds
ago. The driver has not received any packets from the server.
java.lang.NullPointerException
is simply a connection error. Normally I'd suggest that you should try accessing your MySQL DB through the MySQL command line client or Workbench to check if it's up but here it like you haven't actually specified a host for the MySQL server (as you have a Null Pointer Error there).
You'll need to properly specify your host name in the form you provide in the OP. So to connect to a database called "some_db_name" on a local instance of MySQL as root (with the default blank password) you'd use:
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/some_db_name","root", "");
The #'%' part means the user can access from anywhere. Otherwise you can lock a user down a specific IP address or host name. It's possible to create a user role #% with lower privileges than one with the same user name but at a specific IP so then if that user logs in at that location they then get the roles of the specific IP. It has no relation to the error you're seeing here and it's what the default behaviour is for any added user unless they're specifically added with a host mask.
SOLVED! I finally found the sollution.
My hosting provider had changed the settings for the default user on their new platform. (I wasn't able to change it). I have done a lot of testing on several different platforms that the provider has to offer.
-In their new system, I wasn't able to do anything because the user didn't automatically have the right priviliges...
In their old system, I was able to accesss everything (since the user had "dba" (database admin) rights).
Thanks for all the input guys.