I have this program in java to connect to a SQL Server
server = "ZF-SQL-MTRAZDB.NIS.LOCAL"
dbName = "MRAZ"
nameBaseDatos = "CD_LO"
table = "dbo.CD_LO_DATA"
user = "user"
password = "Pass"
url = "jdbc:sqlserver//"+ server + "\\" + dbName + "jdatabaseName=" + nameBaseDatos
driver = "com.microsoft.sqlserver.jdbc_SQLServerDriver"
Now I have to do the same with Visual C# 2010 in Windows XP
How can I do this program?? Because in java use JDBC, Should I also use JDBC?
Thanks for all!
The ConnectionString is similar to an OLE DB connection string, but is not identical. Unlike OLE DB or ADO, the connection string that is returned is the same as the user-set ConnectionString, minus security information if the Persist Security Info value is set to false (default). The .NET Framework Data Provider for SQL Server does not persist or return the password in a connection string unless you set Persist Security Info to true.
You can use the ConnectionString property to connect to a database. The following example illustrates a typical connection string.
"Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)"
Use the new SqlConnectionStringBuilder to construct valid connection strings at run time.
private static void OpenSqlConnection()
{
string connectionString = GetConnectionString();
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
+ "Integrated Security=true;";
}
Data Source or Server orAddressorAddrorNetwork Address
: The name or network address of the instance of SQL Server to which
to connect. The port number can be specified after the server name :
server=tcp:servername, portnumber`
The Initial Catalog or Database : The name of the database. The database name can be 128 characters or less.
The Integrated Security or Trusted_Connection : When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication. Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true. If User ID and Password are specified and Integrated Security is set to true, the User ID and Password will be ignored and Integrated Security will be used.
and other items
I hope this help you :) .
Related
I've got an Azure SQL Server database that I'm connecting to via JDBC, but want to connect instead to my SQL Server "localhost". In SSMS, I connect to localhost without needing a password. So, do I still need to enter a password in Java?
I have a code like this :
String connectionUrl =
"jdbc:sqlserver://etcetc.database.windows.net:1433;"
+ "database=med;"
+ "user=windersan#salemimed;"
+ "password=********;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
// + "hostNameInCertificate=*.database.windows.net;"
+ "loginTimeout=30;";
How do I change this to connect instead to localhost?
Just replace the etcetc.database.windows.net by localhost and replace the port number 1433 by the number that you are using.
I have used SQLServerDataSource class to make the work easier. You can also create a string URL and set it in the DriverManger.getConnection().
Try with this code :
SQLServerDataSource dataSource = new SQLServerDataSource();
dataSource.setUser("windersan#salemimed");
dataSource.setPassword("********");
dataSource.setServerName("localhost");
// set the port number of your system below.
dataSource.setPortNumber(1433);
dataSource.setDatabaseName("med");
dataSource.setEncrypt(true);
dataSource.setHostNameInCertificate("*.database.windows.net");
dataSource.setTrustServerCertificate(false);
Connection connection = dataSource.getConnection();
Please refer to this links down below for more info.
Microsoft Docs - ISQLServerDataSource Interface - This contains the list of methods that you can use to set the various properties in the datasource.
Microsoft Docs - How to work with the connection - This contains examples of the possible ways to connect to a SQL Server database.
the first line of your concatenated string contains the url etcetc.database.windows.net:1433 this is the location of the database server, and the bit you should change.
Also, it might be worth doing a google search on connecting to SqlServer with JDBC to see if there are any examples out there.
I need to connect to a database on my file system but I have noticed that JDBC is referring to a database in memory, any time I want to check the tables need to write a code to retrieve their data, currently I have a SQLite database.
I used the following code but even when I enter a correct address it does not connect to my database.
String sDriver = "org.sqlite.JDBC";
String Database = "users/documents/DB.sqlite";
String sJdbc = "jdbc:sqlite";
Move your database (DB.sqlite) in your working directory. Then use this url to connect to it :
jdbc:sqlite:DB.sqlite
Using JDBC, it will be something like this :
String driver = "org.sqlite.JDBC";
Class.forName(driver);
String dbUrl = "jdbc:sqlite:DB.sqlite";
Connection connection = DriverManager.getConnection(dbUrl);
I'm working on a front end for a database I have set up and I was wondering if I just use this code will my password and username show up in plain text if some one is sniffing?
String url = "jdbc:mysql://" + address + "/table";
String user = user_Name;
String password = complete_Password;
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (SQLException ex) {
System.out.println(ex);
}
That depends on how the JDBC driver is implemented, the MySQL JDBC driver will not transmit your password in clear text. You can see this happening at the MysqlIO class (look for the changeUser method).
You can also see the various types of authentication MySQL offers (including the very unsafe clear text passwords over the wire) at it's client-server protocol documentation.
I seriously doubt any vendor produced database driver out there will send your password data as clear text over the wire. At least I know the MySQL and PosgreSQL JDBC drivers will not do this. PostgreSQL, for instance, will generate a hash of your password and send it.
JDBC is merely an API. every JDBC driver implementation is different, so this would be up to the particular driver that you were using.
I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
So the problem is that I want to make a connection to a msaccess database that has password every time you open it.
The password works if I directly open the access file.
I can make the connection if I remove the password, which means my code works if there is no password involve
The password was set using Set database Password in the database tools
MS Access 2007(but i used the .mdb)
Here's the code
String dbFile = "db.mdb";
String connectionString = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
String driverID = ";DriverID=22;READONLY=true;pwd=qwer}";
if (CONNECTION == null || CONNECTION.isClosed()) {
dbURL = connectionString + dbFile.trim() + driverID;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
CONNECTION = DriverManager.getConnection(dbURL);
}
----------------------------------------------
Error Code : java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Not a valid password.
----------------------------------------------
I already tried adding parameters to the get connection but it did not work.
Please Help :)
There are 2 types of passwords for MS Access database files:
user passwords
database password
With a user password, you supply the user name and password in the connection string.
;User Id=admin;Password=;
For a database password, you need a different identifier in the connection string to distinguish the password as a database password rather than a user password.
;Database Password=MyDbPassword;
Personally I don't use a database password. That feature doesn't offer much in the way of security, so it seems like it's more trouble than it's worth.
Edit: I don't know if it's possible to supply a database password with an ODBC connection. All the connection examples I found used OLE DB when including a database password.
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\somepath\\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", "admin", ""
Perhaps it could work to switch from the current Access ODBC driver to the Microsoft OLE DB Provider for ODBC. Or maybe with the current ODBC driver if you switch
Database Password=MyDbPassword;
to
Jet OLEDB:Database Password=MyDbPassword;
I don't know. But seems to me the database password is just getting in your way here. You already know you can connect if you remove the password from the database.