prepareStatement
package com.demo.test;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
public class TestPrepareStatement {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
String url="jdbc:mysql://192.168.10.127:3306/dac101";
Connection conn=DriverManager.getConnection(url,"dac101","welcome");
PreparedStatement pst=conn.prepareStatement("select * from student");
ResultSet rs=pst.executeQuery();
while (rs.next()) {
System.out.println("Roll_No :" + rs.getInt(1));
System.out.println("FNAME :" + rs.getString(2));
System.out.println("LNAME :" + rs.getString(3));
System.out.println("DOB :" + rs.getDate(4));
System.out.println("EMAILiD :" + rs.getString(5));
System.out.println("-----------------------------------");
}
int Roll_No=217;
String fname="Sarpanch";
String ln="patil";
LocalDate dt=LocalDate.of(2024,11,14);
java.sql.Date sdt=Date.valueOf(dt);
String mail = "sarpanch@gmail.com";
PreparedStatement pst1 = conn.prepareStatement("insert into student values(?,?,?,?,?)");
pst1.setInt(1, Roll_No);
pst1.setString(2, fname);
pst1.setString(3, ln);
pst1.setDate(4, sdt);
pst1.setString(5, mail);
int n =pst1.executeUpdate();
if (n>0) {
System.out.println("Data Inserted Succesfully");
}else {
System.out.println("Data Not Inserted");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Comments
Post a Comment