JDBC

 

Write a network program to update and query a database using JDBC

jdbc.java



import java.io.*;
import java.sql.*;
import java.net.*;
public class jdbc
{
	public static void main(String args[]) throws Exception
	{
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection c= 
   DriverManager.getConnection("jdbc:odbc:sample");
		Statement s=c.createStatement();
		String str="insert into students(name,roll) values(?,?)";
		PreparedStatement pstmt=null;
		pstmt=c.prepareStatement(str);
		pstmt.setString(1,"Joseph");
		pstmt.setInt(2,36);
		pstmt.executeUpdate();
		ResultSet rs=s.executeQuery("select * from students");
		while(rs.next())
		{
			System.out.println("Name: "+rs.getString(1));
			System.out.println("Roll No: "+rs.getString(2));
		}
	}
}


Output

Name: Joseph

Roll No: 36

Leave a Reply

Your email address will not be published. Required fields are marked *