1. Create a student table with fields roll number, name, percentage. Insert values in the table. Display all the details of the student table in a tabular format on the screen (using swing).
import javax.sql.*;
import java.sql.*;
import java.io.*;
import javax.swing.*;
class Ass2seta1
{
public static void main(String args[]) throws IOException,SQLException
{
Connection c=null;
Statement stmt=null;
ResultSet rs=null;
PreparedStatement ps=null;
try
{
Class.forName("org.postgresql.Driver");
c=DriverManager.getConnection("jdbc:postgresql://192.168.100.10/tybcs8",
"tybcs8","");
Statement st=c.createStatement();
int n;
if(c==null)
{
System.out.println("Connection not established");
}
else
{
System.out.println("Connection established");
stmt=c.createStatement();
ps=c.prepareStatement("insert into jstud values(?,?,?)");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many students");
n=Integer.parseInt(br.readLine());
for(int i=0;i<n;i++)
{
System.out.println("Enter student roll number");
int id=Integer.parseInt(br.readLine());
System.out.println("Enter name of student");
String name=br.readLine();
System.out.println("Enter percentage of student");
float per=Float.parseFloat(br.readLine());
ps.setInt(1,id);
ps.setString(2,name);
ps.setFloat(3,per);
ps.executeUpdate();
}
rs=st.executeQuery("select * from jstud");
System.out.print("Roll No \t Name \t Percentage\n");
while(rs.next())
{
System.out.println(rs.getInt(1) +"\t\t"+rs.getString(2)+"\t\t"+rs.getFloat(3));
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
0 Comments
If anyone has Doubts or suggestions please let me know