Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xf;
- import java.sql.*;
- public class zx
- {
- private String query;
- private Connection con;
- private Statement state;
- private ResultSet rs;
- private PreparedStatement prep;
- zx()
- {
- con=null;
- state=null;
- rs=null;
- prep=null;
- query= new String();
- }
- public void run()throws Exception
- {
- Class.forName ("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
- state=con.createStatement();
- query="Create table SC(id int not null,name varchar(40),primary key(id) )";
- state.executeUpdate(query);
- }
- public static void main (String[] args) throws Exception{
- zx a=new zx();
- a.run();
- }
- }
- //insert after table creation
- package xf;
- import java.sql.*;
- public class zx
- {
- private String query;
- private Connection con;
- private Statement state;
- private ResultSet rs;
- private PreparedStatement prep;
- zx() throws SQLException, ClassNotFoundException
- {
- Class.forName ("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
- state=con.createStatement();
- rs=null;
- prep=null;
- query= new String();
- }
- public void insert()throws Exception
- {
- query="insert into SC values(1,'cclass')";
- state.executeUpdate(query);
- }
- public static void main (String[] args) throws Exception{
- zx a=new zx();
- a.insert();
- }
- }
- //display
- package xf;
- import java.sql.*;
- public class zx
- {
- private String query;
- private Connection con;
- private Statement state;
- private ResultSet rs;
- private PreparedStatement prep;
- zx() throws SQLException, ClassNotFoundException
- {
- Class.forName ("com.mysql.jdbc.Driver");
- con=DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
- state=con.createStatement();
- rs=null;
- prep=null;
- query= new String();
- }
- public void get()throws Exception
- {
- query= "select *from SC";
- rs=state.executeQuery(query);
- while(rs.next())
- {
- System.out.println(rs.getInt("id")+""+rs.getString("name"));
- }
- }
- public static void main (String[] args) throws Exception{
- zx a=new zx();
- a.get();
- }
- }
Add Comment
Please, Sign In to add comment