import java.sql.*;
import java.util.*;
public class ResultSetMDEx {
public static void main(String s[]) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Properties p= new Properties();
p.put("user", "root");
p.put("password", "admin");
//DriverManager.getConnection(url,userName,password);
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test",p);
Statement st = con.createStatement();//it creates a object and returns Statement obj
ResultSet rs=st.executeQuery("select * from "+s[0]);
//Table name is taken as an command line arg
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Table Name : "+s[0]);
int colcount=rsmd.getColumnCount();
for (int i=1;i<=colcount;i++) {
System.out.print(rsmd.getColumnName(i)+"\t");
System.out.println(rsmd.getColumnTypeName(i));
}//for
con.close();
}//main
}//class
/*
D:\>cd jdbc
D:\jdbc>cd prog
D:\jdbc\prog>cd monday
D:\jdbc\prog\monday>javac ResultSetMDEx.java
D:\jdbc\prog\monday>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.
1.18-bin.jar;.;
D:\jdbc\prog\monday>java ResultSetMDEx emp;
Table Name : emp;
empno INT
ename VARCHAR
sal DOUBLE
hiredate DATE
D:\jdbc\prog\monday>
*/
import java.util.*;
public class ResultSetMDEx {
public static void main(String s[]) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Properties p= new Properties();
p.put("user", "root");
p.put("password", "admin");
//DriverManager.getConnection(url,userName,password);
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test",p);
Statement st = con.createStatement();//it creates a object and returns Statement obj
ResultSet rs=st.executeQuery("select * from "+s[0]);
//Table name is taken as an command line arg
ResultSetMetaData rsmd=rs.getMetaData();
System.out.println("Table Name : "+s[0]);
int colcount=rsmd.getColumnCount();
for (int i=1;i<=colcount;i++) {
System.out.print(rsmd.getColumnName(i)+"\t");
System.out.println(rsmd.getColumnTypeName(i));
}//for
con.close();
}//main
}//class
/*
D:\>cd jdbc
D:\jdbc>cd prog
D:\jdbc\prog>cd monday
D:\jdbc\prog\monday>javac ResultSetMDEx.java
D:\jdbc\prog\monday>set classpath=D:\softwares\MySQL\new\mysql-connector-java-5.
1.18-bin.jar;.;
D:\jdbc\prog\monday>java ResultSetMDEx emp;
Table Name : emp;
empno INT
ename VARCHAR
sal DOUBLE
hiredate DATE
D:\jdbc\prog\monday>
*/
No comments:
Post a Comment