netbeans and microsoft sql server connection exception - sql-server-2008

I am trying to connect to my database, called "Recept". First I had some trouble about ports, but I fixed it. Now I have this code:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://127.0.0.1:1433;"
+ "databaseName=Recept;";
Connection con = DriverManager.getConnection(connectionUrl);
} catch (SQLException e) {
System.out.println("SQL Exception: " + e.toString());
} catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: " + cE.toString());
}
And I get this exception:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:01819eae-5044-426b-a462-645f247003d6
I don't know what my username and password is, this is how I can connect to my server, you can see, I don't need username and password:
Please someone help me, how should I write my "connectionUrl" in java?
Thank you!

To be able to use the Windows Authentication with the SQL Server JDBC driver, you need to load the right authentication dll by adding the sqljdbc_auth.dll (32 bit or 64 bit depending on your JVM) on your java.library.path, and include the connection property integratedSecurity=true in the JDBC url.
See for detailed instruction and background http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated

Can't you really work with JdbcOdbc driver?
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Recept","sa","sasasa");
}
catch(Exception e)
{
e.printStackTrace();
}

Related

unexpected java.lang.NoClassDefFoundError when calling DriverManager.getConnection()

I am writing a Java application which needs to insert some data to MySQL database through JDBC. Here's the related code:
public JDBCDecoder() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Loaded MySQL JDBC driver");
} catch (ClassNotFoundException e) {
System.out.println("Exception attempting to load MySQL JDBC driver");
}
String url = "jdbc:mysql://localhost/db";
Properties props = new Properties();
props.put("user", "root");
props.put("password", "root");
try {
conn = DriverManager.getConnection(url, props);
conn.setAutoCommit(false);
} catch (SQLException e) {
Throwables.propagate(e);
}
....
}
Here's the error stack trace that I got after trying to run the code:
java.lang.NoClassDefFoundError: Could not initialize class oracle.jdbc.OracleDriver
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:249)
at java.sql.DriverManager.getCallerClass(DriverManager.java:477)
at java.sql.DriverManager.getConnection(DriverManager.java:576)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at exportclient.JDBCExportClient$JDBCDecoder.<init>(JDBCExportClient.java:179)
at exportclient.JDBCExportClient.constructExportDecoder(JDBCExportClient.java:604)
at export.processors.GuestProcessor$1.run(GuestProcessor.java:113)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at utils.CoreUtils$1$1.run(CoreUtils.java:259)
at java.lang.Thread.run(Thread.java:680)
which seems weird to me because: 1) I am not trying to connect to Oracle database; 2) actually I do have an ojdbc6.jar (which contains oracle.jdbc.OracleDriver) in my classpath. So I am completely clueless why this error would happen.
Any suggestion will be appreciated. Thanks in advance!
See if this has something to do with your problem: "As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property."

JDBC SQL Connection

I am trying to use JDBC to connect to a mysql database on our school's server. Here is my code:
try
{
// Step 1: Load the JDBC driver.
Class.forName("com.mysql.jdbc.Driver");
System.out.println(1);
// Step 2: Establish the connection to the database.
String url = "jdbc:mysql://csci.lakeforest.edu:3306/csci427_spring11";
System.out.println(2);
Connection conn = DriverManager.getConnection(url,"username","password");
System.out.println(3);
Statement stmt = conn.createStatement (); // Create statement
System.out.println(2);
}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
The 1 and 2 are printing, but then I get this error:
**Got an exception!
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.**
Any help will be appreciated.
Thank you
That usually means:
The server is down
The name of the server is wrong
The port number is wrong

Hey need help with a mysql exception i am getting abt communication link failure

Hey guys, i m running a mysql server on my host machine.
The code is provided below.When i try 2 run the same code from another machine connected to the same router.I get the exception that I hav provided below the code.
Please help.
Code:-
import java.sql.;
import java.io.;
public class Login{
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Connection conn = null;
String url = "jdbc:mysql://"+br.readLine()+":3306/";
String dbName = "p2p";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "123";
System.out.println("Please input Username:");
String user=br.readLine();
System.out.println("Please input Password:");
String pass=br.readLine();
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
String sql="select password from newuser where username='"+user+"'";
Statement stmt=null;
ResultSet rs=null;
try{
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
if(rs.next())
{
if(rs.getString("password").equals(pass))
System.out.println("true");
else
System.out.println("false");
}
else System.out.println("false");
}
catch(Exception e){System.out.println(e);}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Exception:-
com.mysql.jdbc.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driv
er has not received any packets from the server.
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1
112)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:346)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2334)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2
371)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:374)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:305)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at Login.main(login.java:20)
Caused by: java.net.UnknownHostException: 192.168.1.23306: 192.168.1.23306
at java.net.InetAddress.getAllByName0(InetAddress.java:1128)
at java.net.InetAddress.getAllByName0(InetAddress.java:1098)
at java.net.InetAddress.getAllByName(InetAddress.java:1061)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.ja
va:244)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:295)
... 9 more
Are the machines on the same network or allowed in the same VLAN. The error that you are getting means the program cannot even see the MySQL server. Another possibility, check the account on MySQL and make sure that the both servers are in the allowed HOSTS for the account.
Your stack trace has this line:
java.sql.DriverManager.getConnection(DriverManager.java:171) at Login.main(login.java:20) Caused by: java.net.UnknownHostException: 192.168.1.23306: 192.168.1.23306 at
Which means your Java process doesn't know about host address 192.168.1.23306
Hey guys, i got the error.
Instead of using br.readLine() when i put the actual ip of the mysql server m/c the program runs.

Creating a DSN-less connection for MS Access within Java

I'm building a desktop app that needs to communicate with a MS Access database. Now, unless I want to register the DSN for the database on every computer that's going to use the desktop app, I need a way to connect to the database in a DSN-less fashion.
I've searched alot and found some useful links on how to create connection strings and based on that I tried modifying my program based on that but without success.
The code below fails. If i switch the string in the getConnection to "jdbc:odbc:sampleDB" it works, but that's using DSN and not what I want to achieve.
How do I write and use a connection string in java to make a DSN-less connection to a MS Access database?
private Connection setupConnection() throws ClassNotFoundException,
SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("Driver={Microsoft Access Driver (*.mdb)} &_ Dbq=c:\\as\\sampleDB.mdb");
return con;
}
Addition: I'd also like to point out that if anyone has an idea of a way to achieve what I asked for WITH a DSN-connection I'll gladly listen to it!
JDBC connection string shouls start with jdbc: like:
jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\Nwind.mdb
so try with:
Connection con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=c:\\as\\sampleDB.mdb");
If you configure DSN then you can connect to it using simplier connect string: jdbc:odbc:[alias], example:
jdbc:odbc:northwind
I also had this problem and tried many of the suggestions here and on various forums. Finally, I discovered a snippet from one place which led to success connecting and also explains why many of these posts do not work. See http://www.coderanch.com/t/295299/JDBC/databases/jdbc-odbc-DSN-connection-MS
The issue is that there must be a semicolon after the colon at the end of odbc as in jdbc:odbc:;Driver= . This made sense after reading the Oracle documentation on the JdbcOdbc bridge which states that the syntax is jdbc:odbc:dsn; attributes....... Since we are not supplying a DSN, then we need to end with ; before adding attributes.
I am showing below the tests I ran with different connection strings on a Windows 7 Ultimate 32bit machine:
driver= (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
//jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= does lookup to ODBC.ini to find matching driver
try {
connstr= "jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn= DriverManager.getConnection(connstr, "", "");
stmt= conn.createStatement();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:;Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn1= DriverManager.getConnection(connstr, "", "");
stmt1= conn1.createStatement();
dbmeta1=conn1.getMetaData();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:MS Access Database;DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn2= DriverManager.getConnection(connstr, "", "");
stmt2= conn2.createStatement();
dbmeta2=conn2.getMetaData();
}
catch (Exception e){}
try {
connstr= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + fileURI; //64 bit ?? (*.mdb,*.accdb)
conn3= DriverManager.getConnection(connstr, "", "");
stmt3= conn3.createStatement();
dbmeta3=conn3.getMetaData();
}
catch (Exception e){}
stmt1 and stmt3 are null since the connections are null. stmt and stmt2 work. stmt2 uses a connection string I found in the documentation for IBM Tivoli. It works because "MS Access Database" is a valid title in the ODBC registry as a User DSN on my computer.

isValid() call never returns on MySQL JDBC Connection

I have trying to check whether connection is valid or not and using isValid() method of java.sql.Connection. But this method doesn't return and hangs.
Is there anything I am missing or this method requires any configuration? Using mysql-connector-java-5.0.7-bin.jar
Thanks,
Shahid
I don't know why but in 5.0.x versions this method is abstract. It's implemented correctly in 5.1.x.
Using mysql-connector-java-5.1.1-bin.jar:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection (
"jdbc:mysql://localhost/","root", "root");
System.out.println("Is valid? " + connection.isValid(10));
connection.close();
System.out.println("Is valid? " + connection.isValid(10));
} catch (Exception e) {
e.printStackTrace();
}
Returns:
Is valid? true
Is valid? false