/***************************************************************
NAME :
CLASS : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Create a student table with fields roll number, name,percentage. Insert values in the table. Display all the details of the student table using JDBC.
***************************************************************/
import java.sql.*;
public class m1
{
public static void main(String a[]) throws Exception
{
Connection con;
Statement r2;
ResultSet r;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/
student","root","");
r2=con.createStatement();
r2.executeUpdate("create table student(rollno int
primary key,name text,per int)");
r2.executeUpdate("insert into student values(1,'Rutuja',67)");
r2.executeUpdate("insert into student values(2,'Harshali',63)");
r2.executeUpdate("insert into student values(3,'Tejashree',61)");
r=r2.executeQuery("select * from student");
while(r.next())
System.out.println("\nRoll no="+r.getInt(1)+"\nname="
+r.getString(2)+"\npercentage="+r.getInt(3));
}
}
/* OUTPUT:-
Roll no=1
name=Nitish
percentage=67
Roll no=2
name=Dipali
percentage=63
Roll no=3
name=Nalini
percentage=61
*/
***********************************************************************************
/***************************************************************
NAME :
CLASS : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Write a program to display information about the database and list all the tables in the database. (Use DatabaseMetaData).
***************************************************************/
import java.sql.*;
public class mysql1
{
public static void main(String[] args) throws Exception
{
DatabaseMetaData d;
Connection con;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
d=con.getMetaData();
System.out.println("Driver name="+d.getDriverName());
System.out.println("Version is="+d.getDriverVersion());
System.out.println("user name="+d.getUserName());
ResultSet n=d.getTables(null, null, null, new String[]{"TABLE"});
System.out.println("\nInformation about tables");
while(n.next())
{
System.out.println("Table name="+n.getString("TABLE_NAME"));
System.out.println("Table type="+n.getString("TABLE_TYPE"));
}
}
}
/* OUTPUT:-
Driver name=MySQL-AB JDBC Driver
Version is=mysql-connector-java-5.1.14 ( Revision: ${bzr.revision-id} )
user name=root@localhost
Information about tables
Table name=student
Table type=TABLE
*/
***********************************************************************************
/***************************************************************
NAME :
CLASS : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Write a program to display information about all columns in the student table. (Use ResultSetMetaData).
***************************************************************/
import java.sql.*;
public class mysql2
{
public static void main(String[] args) throws Exception
{
ResultSetMetaData d;
Connection con;
Statement s;
ResultSet rs;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
s=con.createStatement();
rs=s.executeQuery("select * from student");
d=rs.getMetaData();
System.out.println("column no="+d.getColumnCount());
for(int i=1;i<=d.getColumnCount();i++)
{
System.out.println("column Name="+d.getColumnName(i));
System.out.println("column type name="+d.getColumnTypeName(i));
}
}
}
/* OUTPUT:-
column no=3
column Name=rollno
column type name=INT
column Name=name
column type name=VARCHAR
column Name=per
column type name=INT
*/
********************************************************************************************
/***************************************************************
NAME :
CLASS : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Write a menu driven program to perform the following operations on student table.
1. Insert
2. Modify
3. Delete
4. Search
5. View All
6. Exit
***************************************************************/
import java.sql.*;
import java.io.*;
public class mysql3
{
public static void main(String[] args) throws Exception
{
Connection con;
ResultSet rs;
Statement t;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/student","root","");
do
{
System.out.println("\n1.Insert\n2.Modify\n3.Delete\n4.Search\n5.View all\n6.Exit");
System.out.println("Enter the choice");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the Rollno");
int roll=Integer.parseInt(br.readLine());
System.out.println("Enter the name");
String n=br.readLine();
System.out.println("Enter the percentage");
int per=Integer.parseInt(br.readLine());
t=con.createStatement();
t.executeUpdate("insert into student values("+roll+",'"+n+"',"+per+")");
break;
case 2:
System.out.println("Enter the roll no for update record");
roll=Integer.parseInt(br.readLine());
System.out.println("Enter the name");
n=br.readLine();
System.out.println("Enter the percentage");
per=Integer.parseInt(br.readLine());
t=con.createStatement();
t.executeUpdate("update student set name='"+n+"',per="+per+" where rollno="+roll);
break;
case 3:
System.out.println("Enter the roll no for delete record");
int no=Integer.parseInt(br.readLine());
t=con.createStatement();
t.executeUpdate("delete from student where rollno="+no);
break;
case 4:
System.out.println("Enter the roll no for search");
no=Integer.parseInt(br.readLine());
t=con.createStatement();
rs=t.executeQuery("select * from student where rollno="+no);
while(rs.next())
{
System.out.println("Roll no="+rs.getInt(1));
System.out.println("name="+rs.getString(2));
System.out.println("percentage="+rs.getInt(3));
}
break;
case 5:
t=con.createStatement();
rs=t.executeQuery("select * from student");
while(rs.next())
{
System.out.println("Roll no="+rs.getInt(1));
System.out.println("name="+rs.getString(2));
System.out.println("percentage="+rs.getInt(3));
}
break;
case 6:
System.exit(0);
break;
}
}while(true);
}
}
/* OUTPUT:-
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
1
Enter the Rollno
4
Enter the name
Rutuja
Enter the percentage
85
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
5
Roll no=1
name=Pallavi
percentage=67
Roll no=2
name=Vinaya
percentage=63
Roll no=3
name=Trupti
percentage=61
Roll no=4
name=Rutuja
percentage=85
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
2
Enter the roll no for update record
4
Enter the name
Rutuja
Enter the percentage
35
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
3
Enter the roll no for delete record
4
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
5
Roll no=1
name=Pallavi
percentage=67
Roll no=2
name=Vinaya
percentage=63
Roll no=3
name=Trupti
percentage=61
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
4
Enter the roll no for search
2
Roll no=2
name=Vinaya
percentage=63
1.Insert
2.Modify
3.Delete
4.Search
5.View all
6.Exit
Enter the choice
6
*/
**********************************************************************************
/***************************************************************
NAME :
CLASS : T.Y.B.Sc(Comp.Sci.)
ROLL NO. :
Program to Read ,Update and Delete any record from Elements table. The table has following fields ( Atomic weight, Name (primary key), Chemical symbol). The input should be provided through Command Line Arguments along with the appropriate data.
The operations are: R : Read, U: Update, D: Delete.
The syntax for Read: R
The syntax for Delete: D name
The syntax for Update: U name new-atomic-weight new-symbol
***************************************************************/
import java.sql.*;
public class mysql4
{
public static void main(String[] args) throws Exception
{
Connection con;
ResultSet rs;
Statement t;
PreparedStatement pr;
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/chemical","root","");
char ch=args[0].charAt(0);
switch(ch)
{
case 'R':
t=con.createStatement();
rs=t.executeQuery("select * from element");
while(rs.next())
{
System.out.println("name="+rs.getString(2));
System.out.println("automic wt="+rs.getInt(1));
System.out.println("symbol="+rs.getString(3));
}
break;
case 'D':
String name=args[1];
t=con.createStatement();
t.executeUpdate("delete from element where name='"+name+"'");
break;
case 'U':
name=args[1];
int awt=Integer.parseInt(args[2]);
String sm=args[3];
t=con.createStatement();
t.executeUpdate("update element set automicwt="+awt+",chemical_sym='"+sm+"' where name='"+name+"'");
break;
}
}
}
/* OUTPUT:-
[root@localhost setb]# java mysql4 R
name=iron
automic wt=132
symbol=al
name=silver
automic wt=32
symbol=s
name=mercury
automic wt=52
symbol=hg
name=zink
automic wt=52
symbol=zn
2:
[root@localhost setb]# java mysql4 D iron
[root@localhost setb]# java mysql4 R
name=silver
automic wt=32
symbol=s
name=mercury
automic wt=52
symbol=hg
name=zink
automic wt=52
symbol=zn
3:
[root@localhost setb]# java mysql4 U silver 32 ag
[root@localhost setb]# java mysql4 R
name=silver
automic wt=32
symbol=ag
name=mercury
automic wt=52
symbol=hg
name=zink
automic wt=52
symbol=zn
*/
No comments:
Post a Comment