I am trying to connect to a remote oracle database in java stored procedure in a traditional way. I am able to call the java stored procedure successfully. But I receive the exception "Missing In or Out parameter at Index 1" while making the connection. I am not sure how this error is related to making a connection in any way. But I don't have any other piece of code in java.
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#host:port:servicename","user","pwd");
}catch(Exception e){
//(Missing In or Out parameter at index 1)
//logged the error in the local database table
}
Can someone please help me where I need to make a change to connect to another oracle database.
Connection URL for servicename and sid differ.
Format:
With SERVICE NAME: jdbc:oracle:thin:#//host:port/ServiceName
With SID: jdbc:oracle:thin:#host:port:SID
Related
I'm working on a project and I have put my database folder in project folder. How can I make a database connection to any directory rather than just default MySQL dir in Java?
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
String UserName = "root";
String Password = "admin";
Connection con = null;
try {
con = DriverManager.getConnection(MySQLURL,UserName,Password);
if (con != null) {
System.out.println("Database connection is successful !!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
When doing this, I get this error:
java.sql.SQLSyntaxErrorException: Unknown database 'c:\program files\snakegame'
Your connection URL is wrong
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
I am not sure why your MySQLURL contains C:\Program Files\SnakeGame
The connection URL for the mysql database is
jdbc:mysql://localhost:3306/[DatabaseName]
Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running (we may also use the server's IP address here), 3306 is the port number, and [DatabaseName] is the name of the database created on the MySQL server.
Replace the [DatabaseName] name accordingly after creating the database in MySQL server
Combining localhost:3306/ with C:\\Program Files\\SnakeGame makes little sense for any database - either you're trying to connect to a file-based database (in which case the localhost... part makes no sense) or you're working with a server-based one (in which case the C:\... part makes no sense.
Also, this connection string would make little sense for a file-based database either because you didn't specify a specific file, just a path.
Incidentally, MySQL is server-based, not file-based. It's expecting a database name after the localhost:3306/ part, not a path (hence the error). The physical location of the actual database program is an installation/configuration issue - it has nothing to do with how you actually connect to the database server once it's already running.
Think about it this way: when you call an external database, web service, or web site, do you need to know which physical folder it's deployed to? Obviously not. The physical folders involved are completely irrelevant when calling MySQL or another database like this.
One of the comments pointed this out, but did you intend to use SQlite or some other file-based database here instead?
The mysql database will not connect to the netbeans code. I am using a Mac Device and have installed mySQL on my device (it is running fine on localhost:8080). However, the connection through Java is not working. I believe there may be an error in the following line "conn = Driver Manager..." since it is not executing. I am unsure of how to change the password / what the password is.
Restarting Xampp
Screenshot of connection code
Expected result: user input is sent to mySQL database
Actual result (error): seen in this screenshot
This is the general database connectivity code .
try
{
Class.forName("java.sql.DriverManager");
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/DatabaseName","MysqlUsername","MysqlPassword");
Statement stmt = (Statement) con.createStatement();
String query = "YourMysqlQuery";
stmt.executeUpdate(query);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
// You also have to add 'jdbc driver' in lib folder of your project.
If want us to solve the problem in your code then please provide the actual code you have done , Not the image .
I am connecting to Oracle 11g DB trough my java program.
I am using Service Name and not SID.
addr = jdbc:oracle:thin:#hostIP:1521:ServiceName
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(addr,un,pw);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(SELECT * from Table);
This works great. I am able to connect to DB and retrieve the data.
However, if I pass Service ID instead of Service Name, code doesn't work! I get exception.
I tried solution mentioned here - Java JDBC - How to connect to Oracle using Service Name instead of SID. But i still see the same exception.
addr should be in this way for service Name.
addr = jdbc:oracle:thin:#hostIP:1521/ServiceName
Using the Service Name
Syntax:
jdbc:oracle:thin:#//[host]:[tcpPort]/[service_name]
[host]: host name of the database server
[tcpPort]: database listener port
[service_name]: system identifier for the database
Example:
jdbc:oracle:thin:#//myhost:1525/myserviceDB
Please refer below article
https://docs.oracle.com/cd/E12518_01/central_office/pdf/141/html/co_ig/Output/appendix_url.htm
I am trying to connect to MSSQL server 2008 on my localhost, but I am getting Errors
WARNING: ConnectionID:2 Prelogin error: host 127.0.0.1 port 1434 Error reading prelogin response: Connection reset
this error repeats like 20 times very quickly, then i get
com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset
I suspect this is some wrong configuration of server. I can connect to external servers with no issues. I can also connect via management studio with no problems.
Please help me, I am getting sick of this issue :|
1) TCP protocol for MSSQL 2008 is disabled by default. You need to enable it from SQL Configuration Manager (in the same [Start], Programs folder as MSSQL):
http://msdn.microsoft.com/en-us/library/ms190425%28v=sql.105%29.aspx
2) To use TCP connections (even on localhost!) you need to allow MSSQL in Windows Firewall:
http://technet.microsoft.com/en-us/library/cc646023.aspx
3) You don't necessarily need to use TCP/IP in order to access MSSQL:
http://msdn.microsoft.com/en-us/library/ms187892%28v=sql.105%29.aspx
'Hope that helps!
To connect to MSSQL Server from a Java application, you need to use the JDBC API. The JDBC API provides classes and methods that connect to the database, load the appropriate driver, send SQL queries, retrieve results etc.
HOW TO CONNECT TO THE DATABASE A ‘Connection’ object represents a connection with a database. To establish the connection, use the method ‘DriverManager.getConnection’. This method takes a string containing a URL which represents the database we are trying to connect to. Below is the sample code for establishing a connection:
private String DATABASE_URL = "jdbc:odbc:embedded_sql_app"; // establish connection to database
Connection connection = DriverManager.getConnection( DATABASE_URL,"sa","123" );
Detailed discussion about the Database URL and how to create it can be found in the resource provided at the end of this post.
QUERYING THE DATABASE The JDBC API provides three interfaces for sending SQL statements to the database, and corresponding methods in the ‘Connection’ interface create instances of them. 1. Statement - created by the ‘Connection.createStatement’ methods. A ‘Statement’ object is used for sending SQL statements with no parameters.
2. PreparedStatement - created by the ‘Connection.prepareStatement methods’. A ‘PreparedStatement’ object is used for precompiled SQL statements. These can take one or more parameters as input arguments (IN parameters).
3. CallableStatement - created by the ‘Connection.prepareCall’ methods. ‘CallableStatement’ objects are used to execute SQL stored procedures from Java database applications.
RETRIEVING THE RESULT A ‘ResultSet ‘is a Java object that contains the results of executing a SQL query. The data stored in a ‘ResultSet’ object is retrieved through a set of get methods that allows access to the various columns of the current row. The ‘ResultSet.next’ method is used to move to the next row of the ‘ResultSet’, making it the current row. The following code fragment executes a query that returns a collection of rows, with column ‘a’ as an ‘int’, column ‘b’ as a ‘String’, and column ‘c’ as a ‘float’:
java.sql.Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM Table1");
while (rs.next()) { // retrieve and print the values for the current row
int i = rs.getInt("a");
String s = rs.getString("b");
float f = rs.getFloat("c");
System.out.println("ROW = " + i + " " + s + " " + f); }
This is just a brief introduction on how to interact with a database from Java. For more details on the items discussed above as well as information on passing parameters, executing stored procedures etc. please refer to the following resource: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html#Shahriar_N_Embedding_SQL_in_Java ) Here, you will also find information on how to interact with a database programmatically; i.e. without using SQL. Hope you find this useful.
hye all..
i am currently doing my final year project in java.i want to connect a ms access database using java.But sadly i'm using windows 7 starter that cannot support odbc.do you have any idea i can solve this problem?thank for all your coming idea and help =)
Simply try to have another OS get installed or try to have some of the drivers that support JDBC and you will surely able to work in connecting dtabase with java.
You could try JDBC drivers like these:
http://www.csv-jdbc.com/stels_mdb_jdbc.htm
http://www.hxtt.com/access.html
Both are commercial products.
A free option would be to migrate the database to SQL Server Express and then use the SQL Server JDBC drivers to connect to that.
I used the sun.jdbc.odbc.JdbcOdbcDriver to connect to a MS Access database. Have that in the same directory as the class file and it should work. Although it should come already installed in the Java SDK.
This is a sample of a practice program I made a while ago.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
// Establish a connection
Connection connection = DriverManager.getConnection
("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=(MS ACCESS DATABASE DIRECTORY)");
System.out.println("Database connected");
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery
("select f_name, l_name from Test where f_name = 'Luke'"); // For example
// Iterate through the result and print the results
while (resultSet.next())
System.out.println(resultSet.getString(1) + "\t" + resultSet.getString(2) );