JDBC MYSQL

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import org.junit.Test;


import cn.qxc.domain.User;




public class Demo1 {

public  static void main() throws SQLException, ClassNotFoundException
{
String url = "jdbc:mysql://localhost:3306/jdbc";
String username="root";
String password="1281";
Connection conn=null;
Statement st=null;
ResultSet rs = null;
try{
//1.加载驱动
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("com.mysql.jdbc.Driver");

//2.获取链接
conn=DriverManager.getConnection(url, username, password);

//3.获取项数据库发送sql语句的statement对象
st= conn.createStatement();

//4.向数据库发送sql
rs = st.executeQuery("select * from users");

//5.从结果集中获取数据
while(rs.next())
{
System.out.print("id = "+rs.getInt("id")+"  ");
System.out.print("name = "+rs.getString("name")+"  ");
System.out.print("password = "+rs.getString("password")+"  ");
System.out.print("email = "+rs.getString("email")+"  ");
System.out.println("birthday = "+rs.getDate("birthday")+"  ");
}

//6.释放资源
}finally{

if(rs!=null)
{
try
{
rs.close();
}catch(Exception e)
{
e.printStackTrace();
}
}



if(st!=null)
{
try
{
st.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}

}
}

你可能感兴趣的:(mysql,jdbc)