首页 > 数据库技术 > 详细

java 读取mysql库表数据

时间:2016-03-20 21:16:44      阅读:314      评论:0      收藏:0      [点我收藏+]

 

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();    
}
}

}

 

java 读取mysql库表数据

原文:http://www.cnblogs.com/testway/p/5299454.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!