zoukankan      html  css  js  c++  java
  • 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();    
    }
    }
    
    }
  • 相关阅读:
    LeetCode Flatten Binary Tree to Linked List
    LeetCode Longest Common Prefix
    LeetCode Trapping Rain Water
    LeetCode Add Binary
    LeetCode Subsets
    LeetCode Palindrome Number
    LeetCode Count and Say
    LeetCode Valid Parentheses
    LeetCode Length of Last Word
    LeetCode Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/testway/p/5299454.html
Copyright © 2011-2022 走看看