2016-04-22jdbc-mysql-java
Posted 暴走灬青春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2016-04-22jdbc-mysql-java相关的知识,希望对你有一定的参考价值。
//需求:遍历学生表的姓名
@Test
public void test() throws SQLException, ClassNotFoundException
//注册驱动
//DriverManager.registerDriver(new Driver());
String url = "jdbc:mysql://localhost:3306/mytest";
String user = "root";
String password = "123456";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
Class.forName("com.mysql.jdbc.Driver");
//获得连接
conn = DriverManager.getConnection(url, user, password);
//发送sql语句的对象
String sql = "select * from student;";
stmt = conn.prepareStatement(sql);
//结果集
rs = stmt.executeQuery();
while(rs.next())
String name = rs.getString("name");
System.out.println(name);
catch (Exception e)
finally
try
if(rs != null)
rs.close();
rs = null;
catch (Exception e2)
try
if( stmt != null)
stmt.close();
stmt = null;
catch (Exception e2)
try
if( conn != null)
conn.close();
conn = null;
catch (Exception e2)
以上是关于2016-04-22jdbc-mysql-java的主要内容,如果未能解决你的问题,请参考以下文章
lazarus 2016 2月18 4:22:35 支持android开发了, 既ios,linux,macosx,window,web 后 囊括一切啦。 哈哈