java连接mysql

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值