Pages

Java Tutorials

Java Tutorials
Java Tutorials

Thursday, 7 November 2013

JDBC Program to Save Data in Database

import java.io.*;
import java.sql.*;
import java.sql.Connection.*;
import java.sql.PreparedStatement.*;
class save
{
        public static void main(String args[])
        {
                String n,c;
                Connection cn;
                PreparedStatement pst;
int x;
                try
                {
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("enter name & city");
n=br.readLine();
c=br.readLine();


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

cn=DriverManager.getConnection("jdbc:odbc:ucer","","");

pst=cn.prepareStatement("insert into school(name,city) values(?,?)");
pst.setString(1,n);
pst.setString(2,c);
x=pst.executeUpdate();
if(x==1)
{
System.out.println("Record has been saved");
}

                }
                catch(Exception xx)
{
System.out.println("please check the data "+xx.getMessage());
}
        }
}

No comments:

Post a Comment