Click here to load reader

Lecture 1. java database connectivity

Embed Size (px)

DESCRIPTION

Computer Science Java and Ms Sql Server (JDBC)

Citation preview

  • 1. Java Database ConnectivityNangarhar University Computer SciencefacultyFall semester 2014Waheedullah Sulaimankhail

2. Overview1. JDBC Introduction2. JDBC Configuration3. Practical Examples with JDBC 3. 1. JDBC introduction JDBC stands for Java DatabaseConnectivity, which is a standardJava API for database-independentconnectivity between the Javaprogramming language and a widerange of databases. The JDBC library includes APIs for eachof the tasks commonly associated withdatabase usage 4. How JDBC works1. Registering JDBC Driver2. Making a connection to a database3. Creating SQL or MySQL statements4. Executing that SQL or MySQL queriesin the database5. Viewing & Modifying the resultingrecords 5. 2. JDBC Configuration We will use eclipse and Microsoft Sql Server So please install Eclipse and Ms Sql Server 6. Configuration . Add Sql Server Path to environment variable. 7. Configuration .. Download JDBC driver for Ms Sqlserver Download the sqljdbc__enu.exe to atemporary directory. Run sqljdbc__enu.exe (unzip it) 8. Configuration Copy sqljdbc_auth.dll From this folder : Microsoft JDBC Driver 4.0for SQL Serversqljdbc_4.0enuauthx64 To C: windows / System32 folder 9. Add JDBC Driver for Ms Sql Server to Projectproperties in Eclipse 10. 3. Practical Examples with JDBC Setting Up JDBC Examples 3 important steps to set up JDBC1. Write the import statements import java.sql.*;2. Register JDBC Driver Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");3. Creating connection and opening it Connection con =DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=db_name;integratedSecurity=true","User","Pw"); 11. Executing queries with JDBC After jdbc is set up we need to write queries andexecute them. In sql server we create query and execute them. However in JDBC we need to create a statementand initialize the statement with object ofconnection class. And execute the query with an object ofstatement class. 12. Create statement and execute createtable query Statement st = con.createStatement(); String query = "CREATE TABLE student " +"(id INTEGER not NULL, " + " nameVARCHAR(255), " +" fname VARCHAR(255), " + " gradeVarchar(255), " +" PRIMARY KEY ( id ))" ; St.executeUpdate(query); System.out.println(New Table with name studentis created ); 13. Creating Select Query and executing Statement st= con.createStatement(); String query= "SELECT id, first, last, age FROMEmployee"; .. = st.executeQuery(query); executeQuery method returns a dataset so weneed to store the outcome in a dataset We will rewrite the executeQuery method asabove ResultSet rs = st.executeQuery(query); Now we need to fetch the data from our result set 14. Fetching data from data set Rs.next() // will fetch the next record (next row) To get each record we will run a while loop till thelast record is reached While(rs.next()) { int id = rs.getInt("id"); // this will fetch the columnnamed id // the same for all other columns which we wantto fetch from this record System.out.println(ID :+id); // show the record } 15. Clearing the Environment Rs.close(); St.close(); Con.close(); 16. Thanks for attentionDownload these slides from :https://app.box.com/s/62nb671lj6vsz23ylw7v