Getting started with JDBC
Chapters
Streaming Data
PreparedStatement object has ability to input and output streams to supply parameter data.
Below are following methods:
- setAsciiStream():ASCII values supply large.
- setCharacterStream():UNICODE values supply large.
- setBinaryStream():binary values large.
Example
XML file XML_DataFile.xml into database table:
<?xml version="1.0"?>
<USER>
<id>100</id>
<first>chris</first>
<last>A</last>
<Salary>19000</Salary>
<Dob>28-09-1976</Dob>
</USER>
JDBCXMLExample.java compile and run:
import java.sql.*; import java.io.*; import java.util.*; public class JDBCExample { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/USERLOYEE"; static final String USER = "username1"; static final String PASS = "password1"; public static void main(String[] args) { Connection conn1 = null; PreparedStatement pstmtmnt = null; Statement stmtmnt = null; ResultSet rs_1 = null; try{ Class.forName("com.mysql.jdbc.Driver"); Conn1 = DriverManager.getConnection(DB_URL,USER,PASS); stmtmnt = conn1.createStatement(); createXMLTable(stmtmnt); //Open FileInputStream File fs = new File("XML_DataFile.xml"); long fileLength = fs.length(); FileInputStream fis = new FileInputStream(fs); String SQL = "INSERT INTO XML_Data VALUES (?,?)"; pstmt = conn.prepareStatement(SQL); pstmt.setInt(1,100); pstmt.setAsciiStream(2,fis,(int)fileLength); pstmt.execute(); fis.close(); SQL = "SELECT Data FROM XML_Data WHERE id=100"; rs_1 = stmt.executeQuery (SQL); if (rs_1.next ()){ InputStream xmlInputStream = rs.getAsciiStream (1); int c; ByteArrayOutputStream bos1 = new ByteArrayOutputStream(); while (( c = xmlInputStream.read ()) != -1) bos1.write(c); System.out.println(bos.toString()); } // environment Clean-up Rs1.close(); stmtmnt.close(); pstmtmnt.close(); conn1.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ //finally try{ if(stmtmnt!=null) stmtmnt.close(); }catch(SQLException se2){ } try{ if(pstmtmnt!=null) pstmtmnt.close(); }catch(SQLException se2){ } try{ if(conn1!=null) conn1.close(); }catch(SQLException se){ se.printStackTrace(); }//end }//end }//end main public static void createXMLTable(Statement stmt) throws SQLException{ String streamingDataSql = "CREATE TABLE XML_Data " + "(id INTEGER, Data LONG)"; try{ stmt.executeUpdate("DROP TABLE XML_Data"); }catch(SQLException se){ }// do . stmt.executeUpdate(streamingDataSql); } }
Now let us compile above example as follows:
C:\>javac ExampleJDBC.java
Compile and run ExampleJDBC:
C:\>java ExampleJDBC
<?xml version="1.0"?>
<USER>
<id>100</id>
<first>chris</first>
<last>A</last>
<Salary>19000</Salary>
<Dob>28-09-1976</Dob>
</USER>
Description
This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts
- Introduction
- SQL Syntax
- Setting up Environment
- Getting started with some samples
- Connecting to database
- Statements
- Result Sets
- Data Types
- Transactions
- Exception handling
- Batch Processing
- Streaming Data
- More JDBC Examples
Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected
Prerequisites
Understanding of Java language a must to understand various things in this tutorial
Audience
Absolute beginners who wants to get started with JDBC
Author: Subject Coach
Added on: 8th Mar 2015
You must be logged in as Student to ask a Question.
None just yet!