You are on page 1of 5

Practical 8: Perform database operation using Statement,

Prepared, Callable statements


package networking;
import java.sql.*;
public class practical8
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/","siddhiviradi
ya","siddhi");
/*-----statement -----*/
Statement st=con.createStatement();
String sql="create database students5";
st.executeUpdate(sql);
String sql1="use students5";
st.executeUpdate(sql1);
String sql2="create table info2(id int(10) not null primary Key,name
varchar(30))";
st.executeUpdate(sql2);
/*-----preparedstatement -----*/

PreparedStatement stm=con.prepareStatement("insert into student


valuer(?,?)");

stm.setInt(1,122);
stm.setString(2,"siddhi Viradiya");
/*-----callablestatement -----*/
CallableStatement stm1= con.prepareCall("{ call getcall() } ");
ResultSet rs= stm1.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt(2)+ " "+rs.getString(1)+"
"+rs.getString(3));
}
}
catch(Exception e)
{
System.out.println("Exception: "+e);
}
}

You might also like