import java.sql.*;
import java.io.*;
public class MysqlTest {
public static void main(String[] args) {
String tableName=null;//表名
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
try{
Class.forName("com.mysql.jdbc.Driver");//加载驱动程序
System.out.println("OK,DownLoad the driver");
/*school为在MYSQL下建立的数据库名字,03101069是进入数据库的密码*/
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/school","root","03101069");
con.setCatalog("school");
System.out.println("GOOG,Connect the DataBase");
Statement statement = con.createStatement();//数据库操作
System.out.print("请输入表名:");
tableName=input.readLine();
String sql="SELECT * FROM "+tableName;
ResultSet rs = statement.executeQuery(sql);
System.out.print("请输入列名:");
String cName=input.readLine();
// 获得数据结果集合
if(!rs.next())
System.out.println("表名或列名输入有误或到了末尾");
else {
System.out.println("查询结果为:");
do
{
String result=rs.getString(cName);
result=new String(result.getBytes("ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}
rs.close();
con.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
}
输出:
OK,DownLoad the driver
GOOG,Connect the DataBase
请输入表名:student
请输入列名:name
查询结果为:
abc