example consist of type1 type2 type3 and type4 driver
jdbc1 example
import java.sql.*;
import java.io.*;
public class JDBC1
{
public static void main(String[] args) throws ClassNotFoundException,SQLException,IOException
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:odbc:name","scott","tiger");
Statement stmt=con.createStatement();
DataInputStream dis=new DataInputStream(System.in);
try{ while(true)
{
ResultSet rs=stmt.executeQuery("select * from emp");
System.out.println("Please enter the name of the column to be fetched");
String col=dis.readLine().trim();
System.out.println(col);
if(col==null)
break;
rs.next();
while(rs.next())
{
System.out.println(rs.getString(col)+" "+rs.getString(2));
}
}}catch(Exception e)
{System.out.println("error while entering");
}
}
};
jdbc example2 connecting with mysql
import java.sql.*;
public class JDBC2
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/shob","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from login");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
jdbc example 3 with wamp sql little change
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Wampsql {
public static void main(String args[]) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url;
url ="jdbc:mysql://localhost/shob" ;
String user, pass;
user = "root";
pass = "";
Connection con;
Statement stmt;
ResultSet rs;
try {
con = DriverManager.getConnection(url, user, pass);
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM login");
// Get the resultset ready, update the passwd field, commit
//rs.first();
//rs.updateString("message_text", "updated");
//rs.updateRow();
System.out.println(rs.getString(1));
rs.close();
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
}
}
jdbc example 1 more driver
import java.sql.*;
public class JDBC4
{
public static void main(String[] args) throws ClassNotFoundException,SQLException
{
String serverName="esurakshaxp4";
String portNumber="1521";
String sid="oracle";
String url="jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
//String url="jdbc:mysql://localhost/quality_assurance_lms";
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con=DriverManager.getConnection(url,"scott","tiger");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
}
}
solutions for assignment posted update salary
import java.sql.*;
import java.io.*;
public class UpdateTest
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dabctest","scott","tiger");
Statement st=con.createStatement();
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the Employee name :");
System.out.flush();
String str=dis.readLine();
System.out.println("Enter Salary :");
System.out.flush();
int newsal=Integer.parseInt(dis.readLine().trim());
int i=st.executeUpdate("update emp set sal="+newsal+" where ename='"+str+"'");
//System.out.println("update emp set sal="+newsal+" where ename='"+str+"'");
System.out.println("No of Rows Updated :"+i);
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
this is for prepare statement example and assignment solution
import java.io.*;
import java.sql.*;
public class PrepareTest
{
public static void main(String[] args)
{
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
DataInputStream dis=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:dabctest","scott","tiger");
// Class.forName("com.mysql.jdbc.Driver");
//String s="jdbc:mysql://localhost/eSikshak1";
//con=DriverManager.getConnection(s,"root","");
pst=con.prepareStatement("Select * from emp where empno= ? and ename= ?");
dis=new DataInputStream(System.in);
while(true)
{
System.out.println("Enter the empno number :");
System.out.flush();
int eno=Integer.parseInt(dis.readLine().trim());
String enam=dis.readLine().trim();
if(eno==0)
break;
pst.setInt(1,eno);
pst.setString(2,enam);
rs=pst.executeQuery();
while(rs.next())
{
String enumber = rs.getString(1);
String ename = rs.getString(2);
System.out.println(enumber);
System.out.println(ename);
}
}
pst.close();
con.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
rest if some one needed post in comment please
No comments:
Post a Comment