
Dear sir
I will enter the dept code, dept name and insert into dept_mast and save. Pl.
Dept_mast Dept_code Char(2) Primary key (no duplicate key allowed) Dept_name Char(25) **Entry screen** **Department entry** Dept Code : ----- (Check Duplicate not allowed) Name : -------------------- Message :

import java.util.*;
import java.sql.*;
class InsertData{
public static void main(String[] args){
try{
Scanner input=new Scanner(System.in);
System.out.print("DEPT Code: ");
String code=input.nextLine();
System.out.print("DEPT Name: ");
String name=input.nextLine();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from dept where DEPT_NO='"+code+"'");
int count=0;
while(rs.next()){
count++;
}
if(count>0){
System.out.println("Error! Your dept code is not valid!");
}
else{
st.executeUpdate("insert into dept(DEPT_NO,DEPT_NAME) values('"+code+"','"+name+"')");
System.out.println("Data is successfully inserted!");
}
}
catch(Exception e){
System.out.println(e);
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.