package databasecon;
import java.sql.*;
public class Main
{
public static void main(String args[])
{
Connection con =null;
Statement st= null;
PreparedStatement pst=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/college","root","");
st=con.createStatement();
String sqlc="Insert into cse Values(8,'Meenakshi')";
st.executeUpdate(sqlc);
pst=con.prepareStatement("Insert into cse Values(?,?)");
pst.setInt(1, 31);
pst.setString(2, "Divya");
pst.executeUpdate();
con.setAutoCommit(false);
String s1="Insert into cse Values (32,'pritam')";
String s2="Insert into cse Values (33,'Anurag')";
String s3="Insert into cse Values (34,'Vaibhav')";
st.addBatch(s1);
st.addBatch(s2);
st.addBatch(s3);
st.executeBatch();
con.setAutoCommit(true);
System.out.println("Inserted Successfully");
sqlc="Select * from cse";
ResultSet rs= st.executeQuery(sqlc);
while(rs.next())
{
int Roll = rs.getInt("Roll");
String Name = rs.getString("Name");
System.out.println(Name+" Roll no is : "+Roll);
}
}
catch(Exception e)
{
System.out.println("Error is:" +e);
}
finally
{
try
{
if(st!=null)
{
st.close();
}
if(con!=null)
{
con.close();
}
}
catch(SQLException s)
{
System.out.println("Error is" +s);
}
}
}
}
No comments:
Post a Comment