对数据库数据操作之查找

数据库的查找操作:

代码:

package cn.edu.hpu.operate;

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

import com.mysql.jdbc.Connection;

public class Select {
	private static  String driver = "com.mysql.jdbc.Driver";
	private static  String url =  "jdbc:mysql://localhost:3306/数据库名";
	
	public static Connection getConn()
	{
		Connection conn = null;
		try
		{
			Class.forName(driver);
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		try
		{
			conn = (Connection) DriverManager.getConnection(url,"用户名","密码");
			
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return conn;
	}
	public static void main(String[] args) {
	String sql = "select*from student where s_id=具体数值";
	Connection cmn = getConn();
	try
	{
		Statement stmt = cmn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		
		while(rs.next())
		{
			int m1 = rs.getInt(1);//注意数据库中定义数据类型,写相应的set方法
			String m2 = rs.getString(2);
			String m3 = rs.getString(3);
			int m4 = rs.getInt(4);

			System.out.println("查询结果:");
			System.out.println( +m1+"\t"+m2+"\t"+m3+"\t"+m4);
		}
		
	}
	catch(SQLException e)
	{
		e.printStackTrace();
	}
}
}

	


你可能感兴趣的:(对数据库数据操作之查找)