Put your text ad here
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs, or choose the single FAQ by email option for premium answers.
DriverManager thread safe?
Statement and PreparedStatement?
Statement and PreparedStatement?
PreparedStatement?
executeUpdate(String) call!
A: A data store is a general term for a storage system like a relational database that may be used to store and retrieve information. For example, the Enterprise Java Beans (EJB)specifications use the term data store throughout to allow application server vendors to implement storage however they choose. In most cases a data store is implemented in the form of a relational database, but it could take other forms too.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: This will depend on the database you use and the JDBC version you require. To find the latest versions, search Google for JDBC driver and add your database name.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The standard import statement makes a class available to the importing class to use its public variables and method calls. It is certainly possible to import specific JDBC driver implementations and use them in the same way, provided they are available on the application's classpath. However, the Java SQL package and JDBC scheme has been designed to deal with database interfaces in a generic way that is not tied to any single driver implementation. In fact, it is possible to load multiple drivers in a single application.
Though it may be possible to load a specific driver and invoke its connect(String, Properties) method directly, it is far preferable to use the static Class.forName(String) method and get connections through the DriverManager's getConnection() methods. This allows you to configure your application's database driver using external configuration or properties, rather than hard code class references. You only need declare the Java interface types for the driver, connection, statements, rather than vendor-specific classes.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: One of the great strengths of the JDBC API is that the Java code required to load and use a database driver from one vendor is the same as that for any other. The key differences between one vendor's driver and another will be the class name of the driver, and the JDBC URL used to identify it through the DriverManager class. Both of these variables should be set in the application's configuration properties rather than hard coded Java statements. Provided the necessary database driver class packages are available on the application's classpath, the runtime system will be able to load and the run the driver classes.
For Microsoft SQLServer, the database driver class name is com.microsoft.jdbc.sqlserver.SQLServerDriver, the connection URL has the following scheme:
… premium content omitted
Get full access to all FAQs, subscribe now for $40
How do I use JDBC with Microsoft SQLServer?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The DriverManager class is a core part of the java.sql package for Java database connectivity, it provides a registry of JDBC drivers that are loaded and available for use, and methods to get database connections using those drivers. The JDBC specification requires implementations of the java.sql.Driver interface to register themselves with the DriverManager method registerDriver(Driver) when their class is loaded through the Class.forName(String) method.
Once the driver class is loaded and registered, connections can be obtained from the static getConnection() methods of the DriverManager class. The basic getConnection(String) method requires a URL string in the form jdbc:subprotocol:subname, which the driver manager will use to identify the relevant driver and get a connection. Overloaded versions of this method also accept database user name and password strings, or a Properties object containing these details and any other connection details that may be necessary for a specific driver.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
DriverManager thread safe?
A: Yes, the DriverManager getConnection() method is thread safe. However, it is not very efficient for multiple threads to request their own connections from the driver manager, so it is recommended you use a connection pool in a production environment. A connection pool will acquire database connections on behalf of an application; when one thread has finished with a connection it is returned to the pool for re-use and this minimises the new connection time overhead.
The Apache Commons Database Connection Pool (DBCP) library is a robust open source implementation of a connection pool that is widely used. A little extra effort is required to set up, but this will pay off in more rapid database connectivity.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
Statement and PreparedStatement?
A: These SQL package interfaces are declared in the Java API so that your applications do not have to rely on a single specific implementation. The JDBC scheme requires database vendors to provide concrete implementations of these interfaces, and these are obtained via the DriverManager class. When the database driver is registered, the DriverManager accessor methods return database-specific implementations of Driver and Connection, which issue custom Statement, PreparedStatement and ResultSet instances in turn.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
Statement and PreparedStatement?
A: A standard Statement is used to create a Java representation of a literal SQL statement and execute it on the database. The statement input may be any valid SQL, but the class has different methods for inert queries and update statements that change the contents of the database: executeQuery(String) and executeUpdate(String) respectively.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
What's the difference between Statement and PreparedStatement?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: From a data management point of view, it does not seem necessary or sensible to make multiple inserts to a database with a single SQL statement. The nature of the INSERT statement is to create records, and records should be distinct from each other else they would be redundant and inefficient use of storage. An UPDATE statement on existing records can affect multiple records through a single statement, and that is more likely to be an appropriate way to manage your data. Having said that, a PreparedStatement that is used to iterate through a series of insert statements is a perfectly good way to add multiple different records to your database, as below.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
How can I insert multiple records at a time?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
PreparedStatement?
A: Prepared statements are more efficient for managing database actions because they give the driver and the database system itself the opportunity to optimise the queries, and to re-use them. Although the parameters of the query, update or insert may change, the more often the prepared statement is used, the greater the potential saving.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
Why it is better to use PreparedStatement?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The approach to insert simple independent records in multiple tables would be to prepare the two insert Statement objects and call the executeUpdate() method on them one after the other. Depending on your application it may also be appropriate to commit a transaction for these inserts.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Java database programming requires some relatively complicated classpath and database-specific configuration and class loading which are explained elsewhere. This example focuses on creating and executing an SQL insert statement using a PreparedStatement, with values taken from command line parameters. It uses a fixed database driver class name, database URL, user name and password for simplicity.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
How do you make a database insert using JDBC?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: The variables available through the standard system Properties set do not include any custom environment variables stored by the operating system. However, it is possible to add custom variables to this property set with the Java command line flag -D and pass these properties to the database driver manager. With variable substitution, it is then possible to use operating system variables for these values, as detailed below.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
How can I use system variables to make a database connection?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: Developing a database connection pool is a relatively advanced project. You should consider using an existing open source tool like the Apache Commons database connection pool component.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
executeUpdate(String) call!
A: The Statement executeUpdate(String) method takes a single string argument. The string concatenation syntax you have used is incorrect and represents multiple comma separated string arguments, not a single string. The error "cannot find symbol" means that the Java compiler cannot find a Statement method that has this number of string arguments.
This case highlights one of the hazards of concatenating SQL statements in method calls, it can be difficult to see where the Java syntax ends and the SQL syntax begins. It would be much clearer and simpler to use a PreparedStatement in this case and substitute the variables explicitly, as below.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
I get "cannot find symbol" for my executeUpdate(String) call!
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: This exception explains itself; your Web application could not get a connection to the Microsoft SQL Server service. There are many possible causes for a connection failure. Most simply the database server may not be running or there may be no network connection to the machine the database is running on, or it is operating on a different port, for example.
It may help to write a small JDBC utility to test the database connection problem independently from your Web application. It can be easier to produce diagnostic information from a command line application, re-configure and re-deploy than to do the same with a Web application. You will likely find a small but significant configuration difference between your development environment and production environment that can easily be corrected.
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: There is no need to insert relational database records in any particular sequence. When you insert a record the database application will typically add the record to the end of the table. If it is important for your database application to list data in a particular sequence, you would normally use a data field to determine the sort order. For example, you might use the SQL ORDER BY statement to sort the data by a customer's last name and first name:
SELECT * FROM customer ORDER BY last_name, first_name;
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: To select records by date, your database table must have a DATE or TIMESTAMP field to use in the WHERE condition of the query statement. For example, for a product order table you might have a date_ordered field that is a DATE data type. Secondly, since the SQL query input is submitted as a string, you must cast the two dates to DATE data types, as below, split over several lines for clarity.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
How can I select records between two dates?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
A: A database view is a temporary, selective representation of database fields from one or more tables. The view data is held in a structure that behaves like, and can be treated as, a standard database table. The field values held in a view are dynamically updated as the tables they refer to are changed, so the view is always current.
Database views are created by assigning the results of a standard SQL query to a named view, as in the simple example below. Views are typically used to make a simplified extract from a complex data set, so the queries used to create them tend to be relatively complex. View selections may also cast data from one type to another for convenience.
… premium content omitted
Get full access to all FAQs, subscribe now for $40
What is a database view?
Actions: Follow-up, clarify or correct this answer. Submit a new question.
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
About us: site help, text ads, sponsored links and premium content FAQs.