Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
how would i saver the state of a java program so that if i had to give the copy from the dist folder that it would still work on another computer without for instance the other computer having MySQL installed etc
Instead of MySQL, you can use 100% Java relational databases such as Apache Derby, H2 Database engine or the HyperSQL. Starting at Java 6, the SDK included Derby as JavaDB. There is a Getting Started Guide in the Java DB 10 (JDK 8) documentation.
You can include the .jar files in your project and use the databases using a JDBC connection without installing any additional software. Application servers such as Glassfish and JBoss uses, by default, Derby and H2 for the example databases and JMS messages. Usually, administrators change that databases (using a different JDBC URL connection) in production.
Notes on using JavaDb or Derby
Derby (and JavaDB) can run as a network server or an embedded one. The network server allows connections from multiple users/programs. An embedded server only works with the application that starts the engine. Both server types are included in the .jar files and you can control them using JDBC and the library API.
For a network server, you must start the server and use a JDBC URL connection like jdbc:derby://localhost:1527/dbname;create=true where a port and a database are defined or a connection such as jdbc:derby://localhost:1527/c:\temp\myDatabase;create=true with the location of the database. You may check the Vogella's tutorial for Apache Derby
// use the Derby JDBC driver
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
// connect to the database
Connection connect = DriverManager
.getConnection("jdbc:derby://localhost/c:/temp/db/FAQ/db");
// execute a Query
PreparedStatement statement = connect
.prepareStatement("SELECT * from USERS");
ResultSet resultSet = statement.executeQuery();
:
For a embedded server, you must use a different JDBC driver and URL. The Connection URL is like jdbc:derby:MyDbTest;create=true, and does not include a port. There is an explanation in the Derby's documentation.
// use the Embedded Derby JDBC driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// connect to the database
Connection connect = DriverManager
.getConnection("jdbc:derby:MyDBTest;create=true");
// execute a Query
PreparedStatement statement = connect
.prepareStatement("SELECT * from USERS");
ResultSet resultSet = statement.executeQuery();
:
Method 1:
If there is no very complicated data like you would save it in a table of a database, you could use for example a simple config file.
Method 2:
Otherwise you could just use a sqllite database. That's like a database in a single file, that doesn't need a server to run.
Method 3:
You could use a remote database. You would have access to the data from anywhere. (Even without copying the Application every time)
Method 4:
Use a "Java" database, like for example H2.
Method ...: There are many other things you can do, to achieve this!
Related
I want to create a blank Oracle 12c database programmaticaltlly with Java. But if I use getConnection() method in JDBC driver, it does not allow me to run a CREATE DATABASE statement with since I have to mention a spwcific SID/service name for the connection.
Is there a way that I can connect to Oracle server without specifying a service name/SID in the connection string? and create a blank database from within a Java application?
I'm trying to find a URL I could connect to in building a demo app for teaching purposes.
Anyone aware of one I could use with JDBC?
You could probably set up a database in a local file and connect to that via JDBC.
Two examples of database drivers that are freely available are:
SQLite Java, to connect to SQLite databases.
UCanAccess, to connect to MS Access databases.
Then you can connect to it via JDBC like this:
Connection conn = DriverManager.getConnection("jdbc:sqlite://<db file path>");
// or
Connection conn = DriverManager.getConnection("jdbc:ucanaccess://<mdb or accdb file path>");
The respective links contain more in-depth tutorials.
You can connect to a file and then set up the database via Java by executing SQL statements. The result is persisted in the file, which you could then use and redistribute for teaching.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a SQL Server 2012 database in the network of my company.
I can access to this database to observe the tables etc... with SQL Server Management Studio giving the following :
Server type : Database Engine
Server name : xxx-yyy-zzz.eu.company.corp, number_of_port
Authentication : Windows Authentication
Username and password
I also know the name of the database and nothing more.
I would like to connect a new Java program I am writing with Eclipse to this SQL Server database. But I have never connected or even used a SQL Server before so I have no idea how to do it. I understood reading some other posts that I need a driver, but I don't understand where I need to install this driver, which driver and what I need to do to extract data from the database in my Java program.
Could you please tell me more about it ? Thank you :)
I suggest you start here with this Microsoft documentation, Microsoft JDBC Driver for SQL Server.
Looks like you can download the actual driver from Microsoft JDBC Driver 6.0 for SQL Server.
What you need to do may vary depending on the operating system you are using however the above driver looks to be pretty inclusive for Linux, Windows and also works with Azure, Microsoft's cloud offering.
The Programming Guide for JDBC SQL Driver has a number of links explaining what you will need to do and Building the Connection URL describes the actual connect string.
This stack overflow post has a sample program. Java program to connect to Sql Server and running the sample query From Eclipse.
You may also find How to connect to Microsoft SQL Server database using Eclipse to be helpful.
Just remember, you need Eclipse IDE for Java EE developers to access
the database. It contains tools for database development e.g. database
explorer. The Eclipse IDE for Java Developers doesn't contain those
tool by default. FYI, I am using Eclipse Java EE IDE for Web
Developers, Version: Kepler Service Release 2.
Some time back I had problems with Microsoft JDBC Driver and used open source JTDS. I do not remember exact problems, but JTDS worked just fine for me.
So, code to connect to database may look like:
//Not required anymore - just for demonstration. Driver class must be in class path
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection dbCon = DriverManager
.getConnection("jdbc:jtds:sqlserver://{db_host}:[db_port]/{Database Name};domain={user Windows domain};user={user id};password={user password}");
PreparedStatement stmt = dbCon.prepareStatement("SELECT GETDATE()");
ResultSet resSet = stmt.executeQuery();
while (resSet.next()) {
System.out.println(resSet.getString(1));
}
System.out.println("Done");
dbCon.close();
Maven dependency for JTDS driver:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
BTW: Maybe there is a newer version available...
I'm looking for a way to pass web-application transaction information on to the underlying database process. In my Java code I might have a transactional method ReservationService#search(), which runs one or several SQLs. On the DBMS I just see a SPID along with some locks. I'm looking for a way to add a tag "ReservationService#search" to the database process.
jTDS / Sybase ASE have an appName which can be passed in as a connection property. As we're using a connection pool, existing connections are re-used, but to my knowledge the appName is only read on establishing a new connection.
How can I re-set the appName on an already existing connection (without closing/opening)? Or, if that simply is impossible, are there any other ideas to get transactional context information from Java to the DBMS?
Tomcat Webapplication (Java 6)
C3P0 Connection Pool (only supports JDBC 3)
jTDS connecting to Sybase ASE 15
Thanks
Simon
Unfortunately not, it seems that you can only specify that in the URL parameters when you open up the connection but can't be altered afterwords.
You can aways pass in a SessionID of some kind from your Java/Tomecat to all your Sybase queries. For me, this was easy as I use stored procedures for all communications between my Java application and the SQL server. I based my SessionID in Java on the J2EE session.
I'm trying to create a simple connection using the jdbc-odbc bridge:
public static Connection getConnection() {
Connection con =null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
"c:\\myfolder\\accesdbfile.accdb";
con = DriverManager.getConnection(conStr);
} catch(Exception e) {
e.printStackTrace();}
return con;
}
But then I get this exception:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0xa4 Thread 0xec0 DBC 0x2f8574c Jet'.
Any ideas?
Update 24/Mar/2009: Now it's working. Created a User Data Source, and for some reason the exception went away.
As a general question, What would be the best way to handle database connections in Java?
In general, the best way to work with an RDBMS in Java is by using a JDBC driver that is designed to connect directly to the database. Using the JDBC-ODBC bridge tends to be sloww.
If you are trying to do basic read/write manipulations with an Access database, I would also recommend taking a look at the Jackcess library.
To answer your general question I would say the best way to handle database connections in Java is to avoid the JDBC-ODBC bridge. Its OK for testing or learning about JDBC but not for real production use. Also, if you have a data source that does not have its own JDBC driver but it has an ODBC driver then you may not have a choice.
The main reason why I suggest that you stay away from it though is that it makes it difficult to deploy your application. You have to set up the Data Source on the machine that you are running your application from. If you have access to the machine no problem, but suppose you are sending the application off to the client? The pure Java JDBC driver works better for this because it is included as part of your application so once your application is installed it is ready to connect to the data source.
Of course depending on your requirements there are two other types of drivers but thats another discussion.
Go to control panel -- > Administrative tool --> ODBC Data Source Administrator
Add database --> Select "Microsoft Driver(*.mdb, *.accdb)"
Dobule click on new database --> Under "Database" click on "select" --> Select your *.accdb file which you hv created as MS access database.
Say OK and go to your java code
Use:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:filename");
It will surely resolve all your problem.