Java Data Base Connectivity, commonly referred to as JDBC, is an API for the ...
Java Class for Communication: ... SQL server configuration for Java connection.
Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng. Ahmed Abumarasa
Database Lab Lab 8 JDBC AND JAVA CONNECTION.
Data Manipulation Language: Introduction: Java Data Base Connectivity, commonly referred to as JDBC, is an API for the Java programming language that defines how a client may access a database. It provides methods for querying and updating data in a database. JDBC is oriented towards relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the JVM host environment. Java Class for Communication:
Class.forName(String): the method Class.forName(String) is used to load the JDBC driver class. The line below causes the JDBC driver from MSSQLSERVER to be loaded into the application. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
When a Driver class is loaded, it creates an instance of itself and registers it with the DriverManager.
DriverManager .getConnection (String): now when a connection is needed, one of the DriverManager.getConnection() methods is used to create a JDBC connection. Below is example for connection: Connection con = DriverManager.getConnection( "jdbc:sqlserver://ServerIP:port;databaseName=DBName”,username,Password) o o o o o
ServerIP: Database server IP, or Name. Port: port for connection with the database server. DBName: database name which we want to connect with it. Username: username for Database authentication. Password: password for Database authentication.
PreparedStatement: An object that represents a precompiled SQL statement. A SQL statement is precompiled and stored in a PreparedStatement object. This object can then be used to efficiently execute this statement multiple times. PreparedStatement ps = con.prepareStatement(“Select * from student”); PreparedStatement execution: o executeUpdate(): used to insert, update or delete SQL statement, return number of rows affected. o executeQuery(): used with SQL SELECT statement. Return the data in Resultset object. PreparedStatement parameter: use question mark? Instead of parameter to be declare separated from the query string. e.g. String query = "insert into student(sno,fname,lname,bdate,gender) values(?,?,?,?,?)"; ps = con.prepareStatement(query); ps.clearParameters(); ps.setInt(1, sno); ps.setString(2, fname); ps.setString(3, lname); ps.setString(4,bdate); ps.setBoolean(5, sex); ps.executeUpdate();
CallableStatement(): Just like PreparedStatement but it is used for stored procedure. E.g. to call the updatestudent stored procedure with 5 input. String query = "{call updatestudent(?,?,?,?,?)}"; try { cs = con.prepareCall(query); cs.setInt(1, sno); cs.setString(2, fname); cs.setString(3, lname); cs.setString(4,bdate); cs.setBoolean(5, sex); cs.executeUpdate(); } catch (SQLException ex) { }
Result Set: An SQL result set is a set of rows from a database, as well as meta-information about the query such as the column names, and the types and sizes of each column. Depending on the database system, the number of rows in the result set may or may not be known. Usually, this number is not known up front because the result set is built on-the-fly. Precomputations often impose undesired performance impacts. E.g. String query = "select fname,sno from student"; try { ps = con.prepareStatement(query); ResultSet rs = ps.executeQuery(); while(rs.next()){ String name = rs.getString("fname"); int sno = rs.getInt("sno"); System.out.println(name +" "+sno); } } catch (SQLException ex) { ex.printStackTrace(); }
Note: after each PreparedStatement, CallableStatement, or ResultSet we need to close them o o o
rs.close(); ps.close(); cs.close();
SQL server configuration for Java connection. 1. In SQL Server Configuration Manager, in the console pane, expand SQL Server Network Configuration, expand Protocols for , and then double-click TCP/IP. 2. In the TCP/IP Properties dialog box, on the IP Addresses tab, several IP addresses appear in the format IP1, IP2, up to IPAll. One of these is for the IP address of the loopback adapter, 127.0.0.1. Additional IP addresses appear for each IP Address on the computer. Right-click each addresses, and then click Properties to identify the IP address that you want to configure. 3. If the TCP Dynamic Ports dialog box contains 0, indicating the Database Engine is listening on dynamic ports, delete the 0. 4. In the IPn Properties area box, in the TCP Port box, type the port number you want this IP address to listen on, and then click OK. 5. In the console pane, click SQL Server Services. 6. In the details pane, right-click SQL Server () and then click Restart, to stop and restart SQL Server.
Figure 2
Figure 1
Code: You can find sample code in Java in http://site.iugaza.edu.ps/amarasa/database/database-lab/