/*
* query.java
*
* Created on 2006年11月3日, 下午10:00
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author Newtown
*/
import java.sql.*;
import java.io.*;
public class query
{
static {
try {
Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading JDBC Driver");
}catch(ClassNotFoundException e) {
System.out.println("Error loading JDBC Driver");
}
}
public static void main(String[] args) {
String sql=null;
try {
System.out.println("please enter your sql:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
sql=in.readLine(); //读sql语句
}catch(IOException e) { }
try
{
Connection connect=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","19870323");
System.out.println("connection established");
Statement sm=connect.createStatement();
sm.executeUpdate(sql);
ResultSet rs=sm.executeQuery(sql);
ResultSetMetaData rsmd=rs.getMetaData(); //获得ResultSetMetaData对象
int column=rsmd.getColumnCount(); //获得列数
for(int i=1;i<=column;i++) //显示各字段名
System.out.print(rsmd.getColumnName(i)+'/t');
System.out.println("/n---------------------------------------");
while(rs.next()) { //显示表的内容
String str="";
for(int i=1;i<=column;i++) //显示一行
str+=rs.getString(i)+'/t';
System.out.println(str);
}
connect.close(); //关闭对象
sm.close();
rs.close();
} catch (SQLException ex)
{
ex.printStackTrace();
}
}
}