Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TO DISPLAY THE CONTENTS OF THE TABLE:
- import java.sql.*;
- public class Database {
- static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
- static final String DB_URL="jdbc:mysql://localhost/school";
- static final String USER="root";
- public static void main(String[]args) {
- Connection conn=null;
- Statement stmt=null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn=DriverManager.getConnection(DB_URL,USER,"");
- System.out.println("Creating statement...");
- stmt=conn.createStatement();
- String sql;
- sql="SELECT * FROM Teachers";
- ResultSet rs=stmt.executeQuery(sql);
- while(rs.next()) {
- int id=rs.getInt("id_no");
- String name=rs.getString("name");
- String first=rs.getString("address");
- System.out.print("ID:"+id);
- System.out.print(",Name"+name);
- System.out.print(",address:"+first);
- }
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se) {
- se.printStackTrace();
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- try {
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2) {
- }
- try {
- if(conn!=null)
- conn.close();
- }catch(SQLException se) {
- se.printStackTrace();
- }
- System.out.println("Goodbye!");
- }
- }
- }
- OUTPUT:
- Connecting to database...
- Creating statement...
- ID:1,Namepooja,address:xyz ID:10,Namexxx,address:YXZID:19,Nameyyy,address:YzDGoodbye!
- ======================================================================================================
- import java.sql.*;
- import com.mysql.jdbc.Statement;
- public class Names {
- static final String JDBC_DRIVER="comm.mysql.jdbc.Driver";
- static final String DB_URL="jdbc:mysql://localhost/school";
- static final String USER="root";
- static final String PASS="";
- public static void main(String[]args) {
- Connection conn=null;
- Statement stmt=null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn=DriverManager.getConnection(DB_URL,USER,PASS);
- System.out.println("Creating Statement...");
- stmt=(Statement) conn.createStatement();
- String sql;
- sql="SELECT * FROM Teachers";
- ResultSet rs=stmt.executeQuery(sql);
- System.out.print("Name\n");
- while(rs.next()) {
- String name=rs.getString("name");
- System.out.print(name+"\n");
- }
- rs.close();
- stmt.close();
- conn.close();
- }catch(Exception se){
- se.printStackTrace();
- }finally {
- try {
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2) {
- }
- try {
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- System.out.println("\nGoodBye!");
- }
- }
- OUTPUT:
- Connecting to database...
- Creating Statement...
- Name
- pooja
- xxx
- yyy
- GoodBye!
- =====================================================================================================
- import java.sql.*;
- public class Table {
- static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
- static final String DB_URL="jdbc:mysql://localhost/emp";
- static final String USER="root";
- static final String PASS="";
- public static void main(String[]args) throws ClassNotFoundException {
- Connection conn=null;
- Statement stmt=null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to a selected database...");
- conn=DriverManager.getConnection(DB_URL,USER,PASS);
- System.out.println("Connected database successfully...");
- System.out.println("Creating table in given database...");
- stmt=conn.createStatement();
- String sql="Create TABLE emp1"+
- "(id INTEGER not NULL,"+
- "first VARCHAR(20),"+
- "last VARCHAR(10),"+
- "age INTEGER,"+
- "PRIMARY KEY(id))";
- stmt.executeUpdate(sql);
- System.out.println("Created table in given database...");
- }catch(SQLException se) {
- se.printStackTrace();
- }finally {
- try {
- if(stmt!=null)
- conn.close();
- }catch(SQLException se) {
- }
- try {
- if(conn!=null)
- conn.close();
- }catch(SQLException se) {
- se.printStackTrace();
- }
- }
- System.out.println("Goodbye!");
- }
- }
- ================================================================================
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.PreparedStatement;
- public class Preparedstmt {
- static final String JDBC_DRIVER="com..mysql.jdbc.Driver";
- static final String DB_URL="jdbc:mysql://localhost/school";
- static final String USER="root";
- static final String PASS="";
- public static void main(String[]args) {
- Connection conn=null;
- PreparedStatement stmt=null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
- System.out.println("Creating statement...");
- String sql = "UPDATE teachers set name=? WHERE id_no=?";
- stmt = (PreparedStatement) conn.prepareStatement(sql);
- stmt.setString(1, "pillai");
- stmt.setInt(2, 19);
- int rows = stmt.executeUpdate();
- System.out.println("Rows impacted : " + rows );
- sql = "SELECT * FROM teachers";
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- int id = rs.getInt("id_no");
- String name = rs.getString("name");
- System.out.print("ID:" +id);
- System.out.print(", Name " + name);
- }
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- System.out.println("\n Goodbye!");
- }
- }
Add Comment
Please, Sign In to add comment