Sie sind auf Seite 1von 5

Creating and Using Stored Procedures A stored procedure is a kind of function that we create directly inside a database having

set of commands clubbed together for database operations. We have to use certain SQL languages like T-SQL (SQL Server), PL/SQL (Oracle) etc. Using SQL Server Start the SQL Server Management Studio Create your database e.g. b64 Create your tables Customer Table 1. cid int primary key 2. cname varchar - 50 3. mobile char 10 Now create a stored procedure from Programmability Stored Procedure New Stored Procedure
CREATE PROCEDURE SaveCustomer @cid int, @cname varchar(50), @mobile char(10) AS BEGIN INSERT INTO Customer VALUES(@cid,@cname,@mobile); END GO

Execute it Now we can use this stored procedure from Java, .Net etc. For Java, we need JDBC-ODBC Bridge and require a data source name Use default user as sa and its password

Example import java.sql.*; import java.util.*; class CallableTest { public static void main(String args[]) throws Exception { Scanner sc=new Scanner(System.in); System.out.print("Customer ID : "); int cid=sc.nextInt(); System.out.print("Name : "); String cname=sc.next(); System.out.print("Mobile : "); String mobile=sc.next(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn=DriverManager.getConnection("jdbc:odbc:b64","sa","pass"); CallableStatement cs=cn.prepareCall("{call SaveCustomer(?,?,?)}"); cs.setInt(1,cid); cs.setString(2,cname); cs.setString(3,mobile); cs.execute(); cn.close(); } } Creating stored procedure in MySql DELIMITER $$ CREATE PROCEDURE SaveCustomer( cid int, cname VARCHAR(25), mobile CHAR(10) ) BEGIN INSERT INTO customer VALUES(cid,cname,mobile); END$$ DELIMITER ;

import java.sql.*; import java.util.*; class CallableTestMySql { public static void main(String args[]) throws Exception { Scanner sc=new Scanner(System.in); System.out.print("Customer ID : "); int cid=sc.nextInt(); System.out.print("Name : "); String cname=sc.next(); System.out.print("Mobile : "); String mobile=sc.next(); DriverManager.registerDriver(new com.mysql.jdbc.Driver()); Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/b64","root","123456") ; CallableStatement cs=cn.prepareCall("{call SaveCustomer(?,?,?)}"); cs.setInt(1,cid); cs.setString(2,cname); cs.setString(3,mobile); cs.execute(); cn.close(); } }

Multi Threading How to create a thread? Create a class and implement Runnable interface into and override the run() method Create an object of Thread class and the define the object of the class having run() method To define the schedule time for a thread use sleep() method public static void sleep(int ms) throws InterruptedException public static void sleep(int ms, int ns) throws InterruptedException

Example public class MyThread implements Runnable { public MyThread(String tname) { Thread t=new Thread(this); //born state t.setName(tname); t.start(); } public void run() { String tname=Thread.currentThread().getName(); try { if(tname.equals("Tata")) { for(int i=1;i<=10;i++) { System.out.println(tname+" : "+i); Thread.sleep(10); } } else if(tname.equals("Tajmahal")) { for(char ch='A';ch<='Z';ch++) { System.out.println(tname+" : "+ch); Thread.sleep(6); } } else { for(int i=5;i<=100;i+=5) { System.out.println(tname+" : "+i); Thread.sleep(10); } } }catch(Exception ex) {

System.out.println(ex.getMessage()); } } public static void main(String args[]) { new MyThread("Tajmahal"); new MyThread("Tata"); new MyThread("Pataka"); } }

Das könnte Ihnen auch gefallen