0% found this document useful (0 votes)
13 views

JAVA_PRACTICAL18

Uploaded by

birhadepranusha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

JAVA_PRACTICAL18

Uploaded by

birhadepranusha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Gramin Technical and Management Campus

Department of computer engineering

Subject/code: -AJP/22517

Name:-Pranusha Shiddhodhan Birhade Batch:- A Roll no:- 67

DOP: DOS:-

Practical no:-18 Write a program to insert and retrieve data from database using JDBC.
Program code:-

1.write a program to create a student table in db and insert a record in db.


code:
import java.sql.*;

public class jdbcDemo1 { public static


void main(String[] args) {

try {
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
System.out.println("driver loaded");
String url="jdbc:odbc:MSBTE";
Connection cn = DriverManager.getConnection(url);
System.out.println("Connection to the Database created");
Statement st = cn.createStatement();
String str = "CREATE TABLE " + "Student1" +
" (id Integer, name Text(32))"; st.execute(str);
System.out.println("Student table created successfully.");
String insertRecordSQL = "INSERT INTO student1 (id,name) VALUES (1,'student1')";
st.executeUpdate(insertRecordSQL);
System.out.println("Record inserted successfully.");
st.close(); cn.close();

} catch (SQLException s) {
System.out.println("sql error"+s);
}
}
}

Output:

2.Write the output of following code.

Code:
package connect; import
java.sql.*;
class JdbcDemo
{
public static void main(String args[])

{
t
r
y

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest


","root", "bidwai@14");
Statement s = con.createStatement(); s.execute("select
* from stud");
ResultSet rs = s.getResultSet();
String text = " ";
System.out.println("Roll Number \t Name");
while (rs.next())
{
text = text + rs.getInt(1) + "\t\t" + rs.getString(2) + "\n";
}
System.out.pr
int(text);
s.close();
con.close();
} catch (SQLException s)
{
System.out.println("sql error");
}
}
}

Output :

Exercise
1.develop a program to create employee table having columns “emp_id” and
“emp_name”.
Code:
import java.sql.*; public class
employeeTblcrete { public static void
main(String[] args) { try {
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
System.out.println("driver loaded");
String url="jdbc:odbc:MSBTE";
Connection cn = DriverManager.getConnection(url);
System.out.println("Connection to the Database created");
Statement st = cn.createStatement();
String str = "CREATE TABLE " + "employee" +
" (emp_id Integer, emp_name Text(32))";
st.execute(str);

System.out.println("employee table created successfully.");

st.close();
cn.close();

} catch (SQLException s) {
System.out.println("sql error"+s);
}
}
}

Output:

Q.2)Develop a program to display the name and rool_no of students from


“student table” having percentage>70.

Code:
package connect;
import
java.sql.*;
public class
pr18_ex2
{
public static void main(String args[])

t
r
y

{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbtest
","root", "bidwai@14");
Statement s = con.createStatement();
s.execute("select * from stud where percentage > 70");
ResultSet rs = s.getResultSet();
String text = " ";
System.out.println("Roll No \t Name \t\t Marks \t\t Percentage");
while (rs.next())
{
text = text + rs.getInt(1) + "\t\t" + rs.getString(2) + "\t\t" + rs.getString(3) + "\t\t"
+ rs.getString(4) +"\n";
}
System.out.print(text);
s.close();
con.close();
}
catch (SQLException s)
{
System.out.println("sql error");
}
}
}
Output :

You might also like