• java 读取mysql库表数据


    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Arrays;
    
    public class Demo {
    
    static String selectsql = null; 
    static ResultSet retsult = null;
    
    public static final String url = "jdbc:mysql://127.0.0.1/test";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "123456";
    
    public static Connection conn = null;
    public static PreparedStatement pst = null;    
    
    public static void main(String[] args) {
    int paraCount = 5; //读取参数数量
    selectsql = "select * from testpass";//SQL语句    
    
    try {
    Class.forName(name);//指定连接类型
    conn = DriverManager.getConnection(url, user, password);//获取连接
    pst = conn.prepareStatement(selectsql);//准备执行语句
    } catch (Exception e) {
    e.printStackTrace();
    }    
    
    String [] paras = new String [paraCount];
    try {
    retsult = pst.executeQuery();//执行语句,得到结果集
    
    while (retsult.next()) {
    for(int i = 0;i<paraCount;i++){
    paras[i] = retsult.getString(i+2);
    }
    System.out.println(Arrays.toString(paras));
    
    }//显示数据
    retsult.close();
    conn.close();//关闭连接
    pst.close();
    } catch (SQLException e) {
    e.printStackTrace();    
    }
    }
    
    }
  • 相关阅读:
    Python循环语句
    Python简单的语句组
    Jedis 之 初始<一>
    微信小程序登入流程
    微信小程序发起请求
    django数据库迁移时候异常
    Git常用命令总结
    微信小程序自定义组件
    POJ3345 Bribing FIPA
    POJ1947 Rebuilding Roads
  • 原文地址:https://www.cnblogs.com/testway/p/5299454.html
Copyright © 2020-2023  润新知