解决方案 »
- Java内部类
- 关于数组的堆栈
- java 把TextField内容通过按钮事件储存到文本文件中
- 草鸟MM遇到的难题 ,请高手帮忙!
- BufferInputStream的这类Buffer是不是和NIO的缓冲区不是一个概念?
- 求助!关于时间转换的问题
- jbuilderx的安装问题
- 类似http://www.sinago.com/giboview/giboview.asp的棋谱程序用JAVA怎么做 提供思路
- 与Oracle连接时,怎样添加Bolb,Clob字段?急!!!!!
- 一个很弱的applet问题,可是不知道错在何处?
- 这个为什么在主函数里能直接用?
- 下载了Java类库,怎么用啊(求高手速来!)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class SimpleJdbc {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 1 load the db driver
Class.forName("com.mysql.jdbc.Driver");
// 2 get the database conncetion
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/zheng", "root", "root");
if (conn != null) {
System.out.println("you are already connect the mysql database!");
} else {
System.out.println("Sorry you are not connect the mysql database!");
}
//insertARow(conn);
select(conn);
conn.close();
} private static void select(Connection conn) throws SQLException {
// TODO Auto-generated method stub
String sql = "select * from user_list";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString(1));//这里写个例子,只是取得了第一列的值
//System.out.println(rs.getString("列名"));
}
rs.close();
stmt.close();
conn.close();
}// private static void insertARow(Connection conn) throws Exception {
// String sql3 = "select * from user_list";
// Statement stmt = conn.createStatement();
// int erc3 = stmt.executeUpdate(sql3);
// // System.out.println("effected row count:"+effectedRowCount);
// System.out.println("erc3:" + erc3);
// }
}