import java.io.DataInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*
* DemoJdbc.java
*
* Created on December 13, 2010, 10:23 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Naveen
*/
public class DemoJdbc {
/** Creates a new instance of DemoJdbc */
public DemoJdbc() {
}
public static void main(String args[]) {
DataInputStream din=new DataInputStream(System.in);
Connection conn = null;
Statement stmt = null;
try {
System.out.println("Enter the data usn,name,branch,sem,cgpa");
String usn=din.readLine();
String name=din.readLine();
String branch=din.readLine();
String sem=din.readLine();
String cgpa=din.readLine();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.out.println("Unable to Load Class");
}
String url="jdbc:mysql://localhost/studentinfo";
String userName="root";
String password="root";
try {
conn=DriverManager.getConnection(url,userName,password);
}
catch (SQLException ex) {
System.out.println("Unable to Connect to Database");
}
try {
stmt=conn.createStatement();
} catch (SQLException ex) {
System.out.println("Unable to create Statment");
}
boolean sucessful = false;
try {
String query="Insert into student values (" +
"'"+usn+"',"+
"'"+name+"',"+
"'"+branch+"',"+
sem+","+cgpa+")";
System.out.println("\nQuery Framed=="+query);
sucessful = stmt.execute(query);
System.out.println("Query executed sucessfully");
} catch (SQLException ex) {
System.out.println("Query not executed ");
}
} catch (IOException ex) {
System.out.println("Unable to read data from keyboard");
} finally {
try {
conn.close();
} catch (SQLException ex) {
System.out.println("Unable to close Connection");
}
}
}
}
No comments:
Post a Comment