import java.sql.*;
import java.io.*;
public class JDBCConn{
private String url="";
private String username="";
private String password="";
public Connection conn(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =DriverManager.getConnection(url, username, password);
return con;
}catch(ClassNotFoundException cnf){
System.out.println("driver not find:"+cnf);
return null;
}catch(SQLException sqle){
System.out.println("can't connection db:"+sqle);
return null;
}
catch (Exception e) {
System.out.println("Failed to load JDBC/ODBC driver.");
return null;
}
}
public void query(Connection con, String sql){
try{
if(con==null){
throw new Exception("database connection can't use!");
}
if(sql==null) throw new Exception("check your parameter: 'sql'! don't input null!");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rmeta = rs.getMetaData();
int numColumns = rmeta.getColumnCount();
while(rs.next())
{
for(int i = 0;i< numColumns;i++)
{
String sTemp = rs.getString(i+1);
System.out.print(sTemp+" ");
}
System.out.println("");
}
}catch(Exception e){
System.out.println("query error:"+e);
}
}
public void execute(Connection con, String sql){
try{
if(con==null) return;
Statement stmt = con.createStatement();
stmt.executeUpdate(sql);
System.out.println("update executed successly");
}catch(Exception e){
System.out.println("execute error: sql = "+sql);
System.out.println(e);
}
}
public void demo(){
String sSQL="";
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("please input update SQL string");
sSQL=stdin.readLine();
Connection conn = conn();
execute(conn,sSQL);
String sql = "select * from TBL_USER";
query(conn,sql);
conn.close();
}catch(SQLException se){
System.out.println(se);
}catch(Exception e){
System.out.println(e);
}
}
public static void main(String[] arg){
if(arg.length!=3){
System.out.println("use: java JDBCConn url username password");
return;
}
JDBCConn oc = new JDBCConn();
oc.url = arg[0];
oc.username=arg[1];
oc.password=arg[2];
oc.demo();
}
}
|
於 2005-07-09 21:09 收藏文章? || 使用结果集元数据对象获取数据库信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
import java.sql.*;
public class JDBCResultMeta {
private String url="";
private String username="";
private String password="";
public static void main(java.lang.String[] args) {
if(args.length!=4){
System.out.println("use: java JDBCResultMeta url username password tablename");
return;
}
JDBCResultMeta JRM = new JDBCResultMeta();
JRM.url = args[0];
JRM.username=args[1];
JRM.password=args[2];
JRM.getMeta(JRM.conn(),args[3]);
}
public Connection conn(){
try {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(url, username, password);
return con;
}catch(ClassNotFoundException cf){
System.out.println("can't find class"+cf);
return null;
}catch(SQLException sqle){
System.out.println("can't connection db:"+sqle);
return null;
} catch (Exception e) {
System.out.println("Failed to load JDBC/ODBC driver.");
return null;
}
}
public void getMeta(Connection con, String table){
try {
Statement Stm = con.createStatement();
String sql="select * from "+table;
ResultSet rs = Stm.executeQuery(sql);
ResultSetMetaData lineInfo = rs.getMetaData();
System.out.println("*********************RESULT META Comment************************");
int columnCount = lineInfo.getColumnCount();
System.out.println("Column Count :"+columnCount);
for(int i=1;i<columnCount+1;i++){
String columeName = lineInfo.getColumnName(i);
String columeType = lineInfo.getColumnTypeName(i);
System.out.println(columeName+" = "+columeType);
}
}catch (SQLException se) {
System.out.println("SQL Exception: " + se.getMessage());
se.printStackTrace(System.out);
}catch(Exception e){
System.out.println(e);
}finally{
try{
con.close();
}catch(SQLException se){}
}
}
}
|
|
posted on 2007-04-26 21:27
☜♥☞MengChuChen 阅读(12068)
评论(0) 编辑 收藏 所属分类:
JavaBasic